如何把一个XML文件,根据条件生成新的XML文件?
现有如下XML文档FILE1.XML
- <TREENODES>
<treenode id= "001 " text= "门店管理 " >
<treenode id= "011 " text= "打印管理 " >
<treenode id= "111 " text= "中店管理 "/>
</treenode>
<treenode id= "012 " text= "小店管理 "/>
</treenode
<treenode id= "002 " text= "前台收款 " />
<treenode id= "003 " text= "卖货收款 " />
<treenode id= "004 " text= "收款 " />
<treenode id= "005 " text= "前收款 " />
-- </TREENODES>
想输入动态条件如: "001,003,011,111 " 字符串,然后生成有如: FILE2.XML
- <TREENODES>
<treenode id= "001 " text= "门店管理 " >
<treenode id= "011 " text= "打印管理 " >
<treenode id= "111 " text= "中店管理 "/>
</treenode>
<treenode id= "012 " text= "小店管理 "/>
</treenode
<treenode id= "003 " text= "卖货收款 " />
-- </TREENODES>
内容的XML文件
请教在.NET中该如何做?
------解决方案--------------------string s = "001,003,011,111 ";
s = ", " + s + ", ";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath( "1.xml "));
System.Xml.XmlNodeList nodes = doc.SelectNodes( "/TREENODES/treenode ");
foreach(System.Xml.XmlNode n in nodes)
{
if(s.IndexOf( ", " + n.Attributes[ "id "].Value + ", ") == -1)
{
n.ParentNode.RemoveChild(n);
}
}
doc.Save(Server.MapPath( "2.xml "));