保存所有节点属性XML
Save all nodes attributes XML
我编写了一个代码,它读取 XMl
文件,然后使用节点名称构建 XML
文件的树视图。我想知道如何使用属性而不是组件(节点)名称?
例如,在下面的 XML
文件中而不是 action
(s) 我想在树视图 copy
或 paste
中打印,等等,除了前两个父节点(Report
和 test
)。
XML
文件:
<Report version="2.1" xmlns="http://www.froglogic.com/XML2">
<test name="Example">
<action name="delet">
this is delet
</action>
<action name="copy">
this is copy
</action>
<action name="paste">
this is paste
</action>
<action name="manipulate">
this is manipulate
</action>
<action name="copy">
this is copy
</action>
<action name="paste">
this is paste
</action>
<action name="manipulate">
this is manipulate
</action>
<action name="change">
this is change
</action>
</test>
</Report>
和C#
代码:
private void File2_load(object sender, EventArgs e)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(File2Path.Text);
treeView2.Nodes.Clear();
treeView2.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView2.Nodes[0];
AddNode(doc.DocumentElement, tNode);
treeView2.ExpandAll();
treeView2.CheckBoxes = true;
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
更新
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
改变
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
到
xNode = inXmlNode.ChildNodes[i];
TreeNode childTreeNode;
if (xNode.LocalName == "action" && xNode.NodeType == XmlNodeType.Element)
{
childTreeNode = new TreeNode(xNode.Attributes["name"].Value);
}
else
{
childTreeNode = new TreeNode(xNode.Name);
}
inTreeNode.Nodes.Add(childTreeNode);
您可能希望在 NodeType
和 LocalName
上添加进一步检查,具体取决于 XML 输入的复杂性和 [=20] 中可能包含的节点=] 输入.
我编写了一个代码,它读取 XMl
文件,然后使用节点名称构建 XML
文件的树视图。我想知道如何使用属性而不是组件(节点)名称?
例如,在下面的 XML
文件中而不是 action
(s) 我想在树视图 copy
或 paste
中打印,等等,除了前两个父节点(Report
和 test
)。
XML
文件:
<Report version="2.1" xmlns="http://www.froglogic.com/XML2">
<test name="Example">
<action name="delet">
this is delet
</action>
<action name="copy">
this is copy
</action>
<action name="paste">
this is paste
</action>
<action name="manipulate">
this is manipulate
</action>
<action name="copy">
this is copy
</action>
<action name="paste">
this is paste
</action>
<action name="manipulate">
this is manipulate
</action>
<action name="change">
this is change
</action>
</test>
</Report>
和C#
代码:
private void File2_load(object sender, EventArgs e)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(File2Path.Text);
treeView2.Nodes.Clear();
treeView2.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView2.Nodes[0];
AddNode(doc.DocumentElement, tNode);
treeView2.ExpandAll();
treeView2.CheckBoxes = true;
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
更新
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
改变
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
到
xNode = inXmlNode.ChildNodes[i];
TreeNode childTreeNode;
if (xNode.LocalName == "action" && xNode.NodeType == XmlNodeType.Element)
{
childTreeNode = new TreeNode(xNode.Attributes["name"].Value);
}
else
{
childTreeNode = new TreeNode(xNode.Name);
}
inTreeNode.Nodes.Add(childTreeNode);
您可能希望在 NodeType
和 LocalName
上添加进一步检查,具体取决于 XML 输入的复杂性和 [=20] 中可能包含的节点=] 输入.