这是我的xml。
<Document>
<page no="1">
<Paragraph no="1">
<Line>line1</Line>
</Paragraph>
<Paragraph no="2">
<Line>line2</Line>
</Paragraph>
</page>
<page no="2">
<Paragraph no="1">
<Line>line1</Line>
</Paragraph>
<Paragraph no="2">
<Line>line2</Line>
</Paragraph>
</page>
</Document>
我的 C# 代码是
XmlDocument xd = new XmlDocument();
xd.Load(@"H:\Sample-8-final.xml");
XmlNodeList pnodelist = xd.GetElementsByTagName("page");
XmlNodeList xdChildNodeList = xd.ChildNodes;
for (int i = 0; i < pnodelist.Count; i++)
{
XmlNode pageNode = pnodelist[i];
foreach (XmlNode xxNode in pageNode.ChildNodes)
{
if (xxNode.Name.ToString().Trim().Equals("Paragraph"))
{
foreach (XmlNode yyNode in xxNode.ChildNodes)
{
yyNode.ParentNode.RemoveChild(yyNode);
}
}
}
xd.Save(@"H:\Sample-8-final_1.xml");
我需要的输出是
<Document>
<page no="1">
<Paragraph no="1">
</Paragraph>
<Paragraph no="2">
</Paragraph>
</page>
<page no="2">
<Paragraph no="1">
</Paragraph>
<Paragraph no="2">
</Paragraph>
</page>
</Document>
但我的代码产生了错误的结果,如下所示:
<Document>
<page no="1">
<Paragraph no="1">
</Paragraph>
<Paragraph no="2">
<Line>line2</Line>
</Paragraph>
</page>
<page no="2">
<Paragraph no="1">
</Paragraph>
<Paragraph no="2">
<Line>line2</Line>
</Paragraph>
</page>
</Document>
请指导我摆脱这个问题......