日期:2014-05-19  浏览次数:20747 次

用C#操作xml文件问题!急啊!!
我一个xml文件
<root>
    <item>
        <section>
                <number> 1 </number>
        </sction>
  </item>
  <item>
        <sction> <number> 1 </number> </sction>
        <sction> <number> 1 </number> </sction>
  </item>
    <item>
        <section>
                <number> 1 </number>
        </sction>
  </item>
</root>
我现在想删除 <item> 接点下有2个 <section> 接点的 <item> 接点,用c#该怎么写啊????高手们救急啊!

------解决方案--------------------
XmlDocument x = new XmlDocument();
x.LoadXml(@ " <root>
<item>
<section>
<number> 1 </number>
</section>
</item>
<item>
<sction> <number> 1 </number> </sction>
<sction> <number> 1 </number> </sction>
</item>
<item>
<section>
<number> 1 </number>
</section>
</item>
</root> ");
XmlNodeList nodes = x.SelectNodes( "//item ");
foreach (XmlNode n in nodes)
{
if (n.ChildNodes.Count == 2) n.ParentNode.RemoveChild(n);
}

------解决方案--------------------
XmlDocument xdoc = new XmlDocument();
xdoc.Load( "input.xml ");
List <XmlNode> nodesToRemove=new List <XmlNode> ();
foreach (XmlNode item in xdoc.DocumentElement.SelectNodes( "item "))
{
if (item.SelectNodes( "section ").Count == 2)
{
nodesToRemove.Add(item);
}
}
foreach (XmlNode section in nodesToRemove)
{
xdoc.DocumentElement.RemoveChild(section);
}
xdoc.Save(@ "..\..\output.xml ");
------解决方案--------------------
XmlDocument xdoc = new XmlDocument();
xdoc.Load( "input.xml ");
List <XmlNode> nodesToRemove=new List <XmlNode> ();
foreach (XmlNode item in xdoc.DocumentElement.SelectNodes( "item "))
{
if (item.SelectNodes( "section ").Count == 2)
{
nodesToRemove.Add(item);
}
}
foreach (XmlNode section in nodesToRemove)
{
xdoc.DocumentElement.RemoveChild(section);
}
xdoc.Save(@ "..\..\output.xml ");
这样子可以么???????