写了一个提供TreeListNode节点就能够得到文本、属性的类。
    1.问题一-使用此类之前需要提供属性和文本类的columnIndex—但是我暂时没写
    具体体现—nodeName = node.GetDisplayText(“MenuName”.Trim());
    —nodeInnerText = node.GetDisplayText(“value”).Trim();
    —nodeAttributes = node.GetDisplayText(“Tag”).Trim();
    这个怎么找呢?
    <此有缺陷版的使用步骤><手动更改列的名称以对应属性和值的cell内容>
    image.png
    应为这个生成xml是绑定数据源的情况下,因此需要手动设置好TreeList中的栏Like this
    Step1 点击右上角-RunDesigner
    image.png
    Step2-下一大步就是设置好多列,即记住位置,比如说我的Tag属性是第3列,value值也就是XmlNode的文本节点是第4列
    解释了这两句—nodeInnerText = node.GetDisplayText(4).Trim();
    —nodeAttributes = node.GetDisplayText(3).Trim();
    image.png
    Step3-还要检查一下属性栏的格式
    此处需要去看一下我的InfoEdit项目的(一)里面有定义读xml生成DataTable表的格式
    InfoEdit(一)-treelist绑定xml文件

    我在将Xml文件写入DataTable的时候,定义了这样的代码
    意思就是每一个属性的后面会加一个逗号

    1. private string GetNodeAttributes(XmlNode node)
    2. {
    3. string result = null;
    4. if(node.Attributes!=null)
    5. {
    6. foreach(XmlAttribute x in node.Attributes)
    7. {
    8. result += x.Name.ToString() + "=" + x.Value.ToString()+" ";
    9. }
    10. }
    11. else
    12. {}
    13. return result;
    14. }

    比如:xml文件中某一行是这样
    那么属性列就为 ID=01,Price=30 Dollars,Name=AB,(此处注意xml文件用DOM方式读取的时候会自动将属性的节点的双引号去除,所以反向生成xml文件的时候步骤如下:
    step1 以字符char{,}分割属性栏每个属性;
    step2 再次以等号分割单个属性
    step3 将等式左边加上等号再加上双引号和等式右侧还有空格才能正确得到有属性的节点)

    1. 此处还要注意 .Split(char[])和Regex.split(string,string)的区别
    2. 一个是以字符分割字符串得到字符数组,另一个是一字符串分割字符串得到字符数组。string a=“aa,bb,cc”;
    3. 比如说aa,bb,cc可以用a.split(',')即得到arrayList[0]="aa",arrayList[1]="",arrayList[2]="cc"
    4. string b = "aajsbbjscc"
    5. aajsbbjscc利用Regex.Split("js")得到aabbcc
    6. 我就犯了相应的错误,所以需要改一下
    7. 我的字符形式并不是aa,bb,cc而是aa,bb,cc, 还有一个逗号
    8. 因此我直接使用string.split方法的时候经常会出现索引的错误
    9. 所以一定要加一句nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
    1. public string GetAttribute()//得到有属性值,但无Text的节点整体 eg:<node ID="01" Age="dd"></node>
    2. {
    3. string a = null;
    4. string all = null;
    5. if (nodeAttributes != null)
    6. {
    7. nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
    8. attributes = nodeAttributes.Split(',');
    9. foreach (string s in attributes)
    10. {
    11. all += formatAttribute(s);
    12. }
    13. a = string.Format("<{0} {1}></{0}>", nodeName, all, nodeName);
    14. }
    15. return a;
    16. }
    17. private string formatAttribute(string s)
    18. {
    19. string[] array = new string[2];
    20. array = s.Split('=');
    21. return string.Format("{0}=\"{1}\" ", array[0], array[1]);
    22. }
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using DevExpress.XtraTreeList;
    6. using DevExpress.XtraTreeList.Nodes;
    7. using System.Text.RegularExpressions;
    8. namespace TreeListToXml
    9. {
    10. class NodeToXmlElement
    11. {
    12. //字段
    13. private string nodeName;//节点名称
    14. private string nodeInnerText;//节点的文本内容
    15. private int id;//节点的id
    16. private int parentId;//节点的父节编号id
    17. private string nodeAttributes;//先得到column("Tag")中的所有字符
    18. private string[] attributes;//之后经过处理得到分开的属性
    19. private string innerXml;
    20. public NodeToXmlElement(TreeListNode node)
    21. {
    22. //id = Convert.ToInt16(node.GetDisplayText("ID").Trim());
    23. // parentId = Convert.ToInt16(node.GetDisplayText("ParentID").Trim());
    24. nodeName = node.GetDisplayText("MenuName".Trim());
    25. nodeInnerText = node.GetDisplayText(4).Trim();
    26. nodeAttributes = node.GetDisplayText(3).Trim();
    27. }
    28. public string NodeName { get { return nodeName; } set { nodeName = value; } }
    29. public string NodeInnerText{ get { return nodeInnerText; } set { nodeInnerText = value; } }
    30. public int Id { get { return id; } set { id = value; } }
    31. public int ParentId { get { return parentId; } set { parentId = value; } }
    32. public string NodeAttributes { get { return nodeAttributes; } set { nodeAttributes = value; } }
    33. public string[] Attributes { get {return attributes; } set {attributes=value; } }
    34. private bool isHaveAttribute = false;
    35. public bool IsHaveAttribute
    36. {
    37. get
    38. {
    39. if (string.IsNullOrEmpty(nodeAttributes))
    40. { return false; }
    41. else
    42. { return true; }
    43. }
    44. set
    45. {
    46. if (string.IsNullOrEmpty(nodeAttributes))
    47. { isHaveAttribute = false; }
    48. else
    49. { isHaveAttribute = true; }
    50. }
    51. }
    52. private bool isHaveText = false;
    53. public bool IsHaveText
    54. {
    55. get
    56. {
    57. if(string.IsNullOrEmpty(nodeInnerText))
    58. { return false; }
    59. else
    60. { return true; }
    61. }
    62. set
    63. {
    64. if (string.IsNullOrEmpty(nodeInnerText))
    65. { isHaveText = false; }
    66. else
    67. { isHaveText = true; }
    68. }
    69. }
    70. /// <summary>
    71. /// 得到无字节点的单个内容
    72. /// </summary>
    73. /// <returns></returns>
    74. public string GetSingleNodeXml()
    75. {
    76. string a = null;
    77. if (IsHaveAttribute)
    78. {
    79. if (IsHaveText)//有属性有文本
    80. { a = GetAttribute(nodeInnerText); }
    81. else//有属性无文本
    82. { a = GetAttribute(); }
    83. }
    84. else//无属性有文本
    85. {
    86. if (IsHaveText)
    87. {a = GetInnerText();}
    88. else
    89. { a = string.Format("<{0}></{0}>", nodeName, nodeName); }
    90. }
    91. return a;
    92. }
    93. /// <summary>
    94. /// 得到无属性、有文本节点的节点整体 eg<book>AB</book>
    95. /// </summary>
    96. /// <returns></returns>
    97. public string GetInnerText()
    98. {
    99. string a = null;
    100. a= string.Format("<{0}>{1}</{0}>", nodeName, nodeInnerText, nodeName);
    101. return a;
    102. }
    103. /// <summary>
    104. /// 得到有属性值,但无Text的节点整体 eg:<node ID="01" Age="dd"></node>
    105. /// </summary>
    106. /// <returns></returns>
    107. public string GetAttribute()//得到有属性值,但无Text的节点整体 eg:<node ID="01" Age="dd"></node>
    108. {
    109. string a=null;
    110. string all = null;
    111. if (nodeAttributes != null)
    112. {
    113. nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
    114. attributes = nodeAttributes.Split(',');
    115. foreach(string s in attributes)
    116. {
    117. all += formatAttribute(s);
    118. }
    119. a = string.Format("<{0} {1}></{0}>", nodeName, all, nodeName);
    120. }
    121. return a;
    122. }
    123. private string formatAttribute(string s)
    124. {
    125. string[] array = new string[2];
    126. array = s.Split('=');
    127. return string.Format("{0}=\"{1}\" ", array[0], array[1]);
    128. }
    129. public string JustAttributes()
    130. {
    131. string a = null;
    132. if(IsHaveAttribute)
    133. {
    134. nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
    135. attributes = nodeAttributes.Split(',');
    136. foreach (string s in attributes)
    137. {
    138. a += formatAttribute(s);
    139. }
    140. a = " " + a;
    141. }
    142. return a;
    143. }
    144. /// <summary>
    145. /// 有文本节点的属性节点eg:<node ID="01" Age="dd">InnerXml</node>
    146. /// </summary>
    147. /// <param name="nodeInnertext"></param>
    148. /// <returns></returns>
    149. public string GetAttribute(string nodeInnertext)
    150. {
    151. string a = null;
    152. a = GetAttribute();
    153. a = a.Insert(a.IndexOf('>') + 1, nodeInnerText);
    154. return a;
    155. }
    156. /// <summary>
    157. /// 判断节点是否有属性从而在其中添加XML
    158. /// </summary>
    159. /// <param name="node"></param>
    160. /// <param name="InnerXml"></param>
    161. /// <param name="isHaveAttribute"></param>
    162. /// <returns></returns>
    163. public string InsertXml(TreeListNode node,string InnerXml,bool isHaveAttribute)
    164. {
    165. string a = null;
    166. if (isHaveAttribute)
    167. { a = GetAttribute(); }
    168. else
    169. { a = GetInnerText(); }
    170. if(node.HasChildren)
    171. {
    172. a = a.Insert(a.IndexOf('>') + 1, InnerXml);
    173. }
    174. return a;
    175. }
    176. /// <summary>
    177. /// 在有属性值的节点中添加文本;
    178. /// </summary>
    179. /// <param name="innerText"></param>
    180. /// <param name="isHaveAttribute"></param>
    181. /// <returns></returns>
    182. public string InsertText(string innerText,bool isHaveAttribute)
    183. {
    184. string a = null;
    185. if (isHaveAttribute)
    186. { a = GetAttribute(); }
    187. else
    188. {
    189. a = GetInnerText();
    190. }
    191. a = a.Insert(a.IndexOf('>') + 1, innerText);
    192. return a;
    193. }
    194. }
    195. }

    之后可以在里面扩展就行
    使用时需要注意:
    绑定数据源的情况下对TreeList中的节点操作不是直接获取的,需要对数据源解绑及重新刷新
    保存文件的条件也是,只有在点击TreeList中的一个节点才能保存到TreeList中的节点数据
    如果点击的节点有子节点,那么保存的就是此节点及其子节点的xml信息
    只点击没有子节点的节点则只能保存他自己的信息
    因此需要在TreeList中的MouseClick中写

    1. private void treeList2_MouseClick(object sender, MouseEventArgs e)
    2. {
    3. TreeList tree = sender as TreeList;
    4. TreeListNode clickNode = this.treeList2.FocusedNode;
    5. testnode = GetChildrenXml(clickNode);
    6. }

    保存按钮点击中保存

    1. private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
    2. {
    3. this.treeList2.OptionsBehavior.Editable = false;
    4. string filePath = Application.StartupPath + "\\example\\result.xml";
    5. SaveFileDialog s = SaveFile("Xml文件(*.xml)|*.xml");
    6. if(s.ShowDialog()==DialogResult.OK)
    7. {
    8. string XmlString = "";
    9. string indent = " ";
    10. XmlString += indent + "<xmlData>";
    11. XmlString += indent + testnode;
    12. XmlString += indent + "</xmlData>";
    13. XmlDocument xmldoc = new XmlDocument(); //创建空的XML文档
    14. xmldoc.LoadXml(XmlString);
    15. string newFileName = DateTime.Now.ToString("yyyyMMdd") + this.Text;
    16. xmldoc.Save(newFileName);
    17. //xmldoc.Save(s.FileName); //保存
    18. }
    19. XtraMessageBox.Show("保存成功");
    20. }