我的C#windows应用程序中有一个从xml文件加载的树。 我检查树中的一些节点,然后我想创建并保存带有选中树节点的xml文件
XML文件中的示例数据
所选节点必须保存在包含所有元素和属性的xml文件中。 感谢
<menu>
<item id="43BDF924-5E" text="System Management" system="010">
<item id="36A21901-45" text="Basic Information">
<item id="7FA03116-0F" text="Info">
<item id="74713E10-FF" code="AGM-D-1240-01" text="Persons"></item>
<item id="5373F379-E8" code="AGM-D-1260-01" text="Stock"></item>
</item>
</item>
<item id="36A21901-45" text="Google">
<item id="7FA03116-0F" text="sites">
<item id="74876E10-FF" code="MM-D-1240-01" text="Serch"></item>
<item id="0981F379-E8" code="MM-D-1260-01" text="Gmail"></item>
</item>
</item>
</item>
</menu>
答案 0 :(得分:0)
使用xml linq
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
treeView1.Nodes.AddRange( new TreeNode[] {
new TreeNode(
"id=\"43BDF924-5E\" text=\"System Management\" system=\"010\"",
new TreeNode[] {
new TreeNode(
"id=\"36A21901-45\" text=\"Basic Information\"",
new TreeNode[] {
new TreeNode(
"id=\"7FA03116-0F\" text=\"Info\"",
new TreeNode[] {
new TreeNode(
"id=\"74713E10-FF\" code=\"AGM-D-1240-01\" text=\"Persons\""
),
new TreeNode(
"id=\"5373F379-E8\" code=\"AGM-D-1260-01\" text=\"Stock\""
)
})
})
}),
new TreeNode(
"id=\"36A21901-45\" text=\"Google\"",
new TreeNode[] {
new TreeNode(
"id=\"7FA03116-0F\" text=\"sites\"",
new TreeNode[] {
new TreeNode(
"id=\"74876E10-FF\" code=\"MM-D-1240-01\" text=\"Serch\""
),
new TreeNode(
"id=\"0981F379-E8\" code=\"MM-D-1260-01\" text=\"Gmail\""
)
})
})
});
treeView1.ExpandAll();
XElement menu = new XElement("menu");
foreach (TreeNode node in treeView1.Nodes)
{
XElement newChild = new XElement("item");
menu.Add(newChild);
CreateXmlElement(newChild, node);
}
}
public void CreateXmlElement(XElement parentElement, TreeNode parentNode)
{
string pattern = "(?'name'\\w+)=\"(?'value'[^\"]+)";
MatchCollection matches = Regex.Matches(parentNode.Text, pattern);
foreach (Match match in matches)
{
parentElement.Add(new XAttribute(match.Groups["name"].Value, match.Groups["value"].Value));
}
foreach (TreeNode node in parentNode.Nodes)
{
XElement newChild = new XElement("item");
parentElement.Add(newChild);
CreateXmlElement(newChild, node);
}
}
}
}