我想为以下树类型结构生成XML。我附上图片。生成的xml应该是
<services>
<service>
<name>Service 1</name>
<categories>
<category>
<name>Cateogry 1</name>
<methods>
<method>
<name>Method 1</name>
</method>
</methods>
</category>
</categories>
</service>
<service>
<name>Service 2</name>
<categories>
<category>
<name>Cateogry 1</name>
<methods>
<method>
<name>Method 1</name>
</method>
</methods>
</category>
</categories>
</service>
<service>
<name>Service 3</name>
<categories>
<category>
<name>Cateogry 1</name>
<methods>
<method>
<name>Method 1</name>
</method>
</methods>
</category>
</categories>
</service>
</services>
答案 0 :(得分:0)
我解决了我的问题:)
XElement elServices = new XElement("services");
foreach (TreeNode serviceNode in treeView1.Nodes)
{
XElement elService = new XElement("service",
new XElement("name", serviceNode.Text));
XElement elCategories = new XElement("categories");
foreach (TreeNode categoryNode in serviceNode.Nodes)
{
XElement elCategory = new XElement("category",
new XElement("name", categoryNode.Text));
XElement elMethods = new XElement("methods");
foreach (TreeNode methodNode in categoryNode.Nodes)
{
XElement elMethod = new XElement("method",
new XElement("name", methodNode.Text));
elMethods.Add(elMethod);
}
elCategory.Add(elMethods);
elCategories.Add(elCategory);
}
elService.Add(elCategories);
elServices.Add(elService);
}
MessageBox.Show(elServices.ToString());