how to remove all the childnodes of the specified xmlnode in C# 4.0?

veisseRoocrix

New Member
This is my xml.\[code\]<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>\[/code\]My C# code is \[code\]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; 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");\[/code\]my Required output is \[code\]<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>\[/code\]but my code produced wrong result like below:\[code\]<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>\[/code\]Please Guide me to get out of this issue...
 
Back
Top