写了一个提供TreeListNode节点就能够得到文本、属性的类。
1.问题一-使用此类之前需要提供属性和文本类的columnIndex—但是我暂时没写
具体体现—nodeName = node.GetDisplayText(“MenuName”.Trim());
—nodeInnerText = node.GetDisplayText(“value”).Trim();
—nodeAttributes = node.GetDisplayText(“Tag”).Trim();
这个怎么找呢?
<此有缺陷版的使用步骤><手动更改列的名称以对应属性和值的cell内容>
应为这个生成xml是绑定数据源的情况下,因此需要手动设置好TreeList中的栏Like this
Step1 点击右上角-RunDesigner
Step2-下一大步就是设置好多列,即记住位置,比如说我的Tag属性是第3列,value值也就是XmlNode的文本节点是第4列
解释了这两句—nodeInnerText = node.GetDisplayText(4).Trim();
—nodeAttributes = node.GetDisplayText(3).Trim();
Step3-还要检查一下属性栏的格式
此处需要去看一下我的InfoEdit项目的(一)里面有定义读xml生成DataTable表的格式
InfoEdit(一)-treelist绑定xml文件
我在将Xml文件写入DataTable的时候,定义了这样的代码
意思就是每一个属性的后面会加一个逗号
private string GetNodeAttributes(XmlNode node)
{
string result = null;
if(node.Attributes!=null)
{
foreach(XmlAttribute x in node.Attributes)
{
result += x.Name.ToString() + "=" + x.Value.ToString()+" ";
}
}
else
{}
return result;
}
比如:xml文件中某一行是这样
那么属性列就为 ID=01,Price=30 Dollars,Name=AB,(此处注意xml文件用DOM方式读取的时候会自动将属性的节点的双引号去除,所以反向生成xml文件的时候步骤如下:
step1 以字符char{,}分割属性栏每个属性;
step2 再次以等号分割单个属性
step3 将等式左边加上等号再加上双引号和等式右侧还有空格才能正确得到有属性的节点)
此处还要注意 .Split(char[])和Regex.split(string,string)的区别
一个是以字符分割字符串得到字符数组,另一个是一字符串分割字符串得到字符数组。string a=“aa,bb,cc”;
比如说aa,bb,cc可以用a.split(',')即得到arrayList[0]="aa",arrayList[1]="",arrayList[2]="cc"
而string b = "aajsbbjscc"
aajsbbjscc利用Regex.Split("js")得到aa、bb、cc
我就犯了相应的错误,所以需要改一下
我的字符形式并不是aa,bb,cc而是aa,bb,cc, 还有一个逗号
因此我直接使用string.split方法的时候经常会出现索引的错误
所以一定要加一句nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
public string GetAttribute()//得到有属性值,但无Text的节点整体 eg:<node ID="01" Age="dd"></node>
{
string a = null;
string all = null;
if (nodeAttributes != null)
{
nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
attributes = nodeAttributes.Split(',');
foreach (string s in attributes)
{
all += formatAttribute(s);
}
a = string.Format("<{0} {1}></{0}>", nodeName, all, nodeName);
}
return a;
}
private string formatAttribute(string s)
{
string[] array = new string[2];
array = s.Split('=');
return string.Format("{0}=\"{1}\" ", array[0], array[1]);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using System.Text.RegularExpressions;
namespace TreeListToXml
{
class NodeToXmlElement
{
//字段
private string nodeName;//节点名称
private string nodeInnerText;//节点的文本内容
private int id;//节点的id
private int parentId;//节点的父节编号id
private string nodeAttributes;//先得到column("Tag")中的所有字符
private string[] attributes;//之后经过处理得到分开的属性
private string innerXml;
public NodeToXmlElement(TreeListNode node)
{
//id = Convert.ToInt16(node.GetDisplayText("ID").Trim());
// parentId = Convert.ToInt16(node.GetDisplayText("ParentID").Trim());
nodeName = node.GetDisplayText("MenuName".Trim());
nodeInnerText = node.GetDisplayText(4).Trim();
nodeAttributes = node.GetDisplayText(3).Trim();
}
public string NodeName { get { return nodeName; } set { nodeName = value; } }
public string NodeInnerText{ get { return nodeInnerText; } set { nodeInnerText = value; } }
public int Id { get { return id; } set { id = value; } }
public int ParentId { get { return parentId; } set { parentId = value; } }
public string NodeAttributes { get { return nodeAttributes; } set { nodeAttributes = value; } }
public string[] Attributes { get {return attributes; } set {attributes=value; } }
private bool isHaveAttribute = false;
public bool IsHaveAttribute
{
get
{
if (string.IsNullOrEmpty(nodeAttributes))
{ return false; }
else
{ return true; }
}
set
{
if (string.IsNullOrEmpty(nodeAttributes))
{ isHaveAttribute = false; }
else
{ isHaveAttribute = true; }
}
}
private bool isHaveText = false;
public bool IsHaveText
{
get
{
if(string.IsNullOrEmpty(nodeInnerText))
{ return false; }
else
{ return true; }
}
set
{
if (string.IsNullOrEmpty(nodeInnerText))
{ isHaveText = false; }
else
{ isHaveText = true; }
}
}
/// <summary>
/// 得到无字节点的单个内容
/// </summary>
/// <returns></returns>
public string GetSingleNodeXml()
{
string a = null;
if (IsHaveAttribute)
{
if (IsHaveText)//有属性有文本
{ a = GetAttribute(nodeInnerText); }
else//有属性无文本
{ a = GetAttribute(); }
}
else//无属性有文本
{
if (IsHaveText)
{a = GetInnerText();}
else
{ a = string.Format("<{0}></{0}>", nodeName, nodeName); }
}
return a;
}
/// <summary>
/// 得到无属性、有文本节点的节点整体 eg<book>AB</book>
/// </summary>
/// <returns></returns>
public string GetInnerText()
{
string a = null;
a= string.Format("<{0}>{1}</{0}>", nodeName, nodeInnerText, nodeName);
return a;
}
/// <summary>
/// 得到有属性值,但无Text的节点整体 eg:<node ID="01" Age="dd"></node>
/// </summary>
/// <returns></returns>
public string GetAttribute()//得到有属性值,但无Text的节点整体 eg:<node ID="01" Age="dd"></node>
{
string a=null;
string all = null;
if (nodeAttributes != null)
{
nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
attributes = nodeAttributes.Split(',');
foreach(string s in attributes)
{
all += formatAttribute(s);
}
a = string.Format("<{0} {1}></{0}>", nodeName, all, nodeName);
}
return a;
}
private string formatAttribute(string s)
{
string[] array = new string[2];
array = s.Split('=');
return string.Format("{0}=\"{1}\" ", array[0], array[1]);
}
public string JustAttributes()
{
string a = null;
if(IsHaveAttribute)
{
nodeAttributes = nodeAttributes.Substring(0, nodeAttributes.LastIndexOf(','));
attributes = nodeAttributes.Split(',');
foreach (string s in attributes)
{
a += formatAttribute(s);
}
a = " " + a;
}
return a;
}
/// <summary>
/// 有文本节点的属性节点eg:<node ID="01" Age="dd">InnerXml</node>
/// </summary>
/// <param name="nodeInnertext"></param>
/// <returns></returns>
public string GetAttribute(string nodeInnertext)
{
string a = null;
a = GetAttribute();
a = a.Insert(a.IndexOf('>') + 1, nodeInnerText);
return a;
}
/// <summary>
/// 判断节点是否有属性从而在其中添加XML
/// </summary>
/// <param name="node"></param>
/// <param name="InnerXml"></param>
/// <param name="isHaveAttribute"></param>
/// <returns></returns>
public string InsertXml(TreeListNode node,string InnerXml,bool isHaveAttribute)
{
string a = null;
if (isHaveAttribute)
{ a = GetAttribute(); }
else
{ a = GetInnerText(); }
if(node.HasChildren)
{
a = a.Insert(a.IndexOf('>') + 1, InnerXml);
}
return a;
}
/// <summary>
/// 在有属性值的节点中添加文本;
/// </summary>
/// <param name="innerText"></param>
/// <param name="isHaveAttribute"></param>
/// <returns></returns>
public string InsertText(string innerText,bool isHaveAttribute)
{
string a = null;
if (isHaveAttribute)
{ a = GetAttribute(); }
else
{
a = GetInnerText();
}
a = a.Insert(a.IndexOf('>') + 1, innerText);
return a;
}
}
}
之后可以在里面扩展就行
使用时需要注意:
绑定数据源的情况下对TreeList中的节点操作不是直接获取的,需要对数据源解绑及重新刷新
保存文件的条件也是,只有在点击TreeList中的一个节点才能保存到TreeList中的节点数据
如果点击的节点有子节点,那么保存的就是此节点及其子节点的xml信息
只点击没有子节点的节点则只能保存他自己的信息
因此需要在TreeList中的MouseClick中写
private void treeList2_MouseClick(object sender, MouseEventArgs e)
{
TreeList tree = sender as TreeList;
TreeListNode clickNode = this.treeList2.FocusedNode;
testnode = GetChildrenXml(clickNode);
}
保存按钮点击中保存
private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
{
this.treeList2.OptionsBehavior.Editable = false;
string filePath = Application.StartupPath + "\\example\\result.xml";
SaveFileDialog s = SaveFile("Xml文件(*.xml)|*.xml");
if(s.ShowDialog()==DialogResult.OK)
{
string XmlString = "";
string indent = " ";
XmlString += indent + "<xmlData>";
XmlString += indent + testnode;
XmlString += indent + "</xmlData>";
XmlDocument xmldoc = new XmlDocument(); //创建空的XML文档
xmldoc.LoadXml(XmlString);
string newFileName = DateTime.Now.ToString("yyyyMMdd") + this.Text;
xmldoc.Save(newFileName);
//xmldoc.Save(s.FileName); //保存
}
XtraMessageBox.Show("保存成功");
}