0

I want to remove all text nodes (but not any other type of node) from an XML file. How can I do this?

Example Input:

<root>
<slideshow id="1">
<Image>hii</Image>
<ImageContent>this</ImageContent>
<Thumbnail>is</Thumbnail>
<ThumbnailContent>A</ThumbnailContent>
</slideshow>
<slideshow id="2">
<Image>hii</Image>
<ImageContent>this</ImageContent>
<Thumbnail>is</Thumbnail>
<ThumbnailContent>B</ThumbnailContent>
</slideshow>
</root> 

Expected Output:

<root>
<slideshow id="1">
<Image></Image>
<ImageContent></ImageContent>
<Thumbnail></Thumbnail>
<ThumbnailContent></ThumbnailContent>
</slideshow>
<slideshow id="2">
<Image></Image>
<ImageContent></ImageContent>
<Thumbnail></Thumbnail>
<ThumbnailContent></ThumbnailContent>
</slideshow>
</root> 
dtb
  • 213,145
  • 36
  • 401
  • 431
Krishh
  • 4,111
  • 5
  • 42
  • 52
  • I've rephrased your question slightly. Please undo if the result doesn't match what you're looking for. – dtb Oct 13 '11 at 15:43

2 Answers2

7

How about:

var doc = XDocument.Load("test.xml");
doc.DescendantNodes()
   .Where(x => x.NodeType == XmlNodeType.Text ||
               x.NodeType == XmlNodeType.CDATA)
   .Remove();
doc.Save("clean.xml");

EDIT: Note that the above was before I realized that XCData derived from XText, leading to the simpler:

var doc = XDocument.Load("test.xml");
doc.DescendantNodes()
   .OfType<XText>()
   .Remove();
doc.Save("clean.xml");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

This question should help: Linq to XML - update/alter the nodes of an XML Document

You can use Linq to open the document and alter the values or remove the nodes altogether.

Community
  • 1
  • 1
Jamie
  • 3,094
  • 1
  • 18
  • 28