Trimming the XML file
I have a XML file as shown below
<NewDataSet>
- <T>
<P />
<C>1</C>
<M />
</T>
- <T>
<P />
<C>1</C>
<M />
</T>
- <T>
<P />
<C>1</C>
<M />
</T>
- <T>
<P />
<C>1</C>
<M />
</T>
- <T>
<P />
<C>2</C>
<M>44</M>
</T>
- <T>
<P />
<C>2</C>
<M>45</M>
<开发者_如何学编程/T>
- <T>
<P />
<C>2</C>
<M>46</M>
</T>
</NewDataSet>
Question - Basicall i should remove the block
<T>
<P />
<C>1</C>
<M />
</T>
that does not have <M>
Value
Load the whole document into an XmlDocument
(or XDocument
), select nodes with an XPath like /T[not(./M)]
and delete them.
You might want to take a look into Linq-to-XML
You could query for all XElements containing an empty M Element, and remove those from their parent XML Element.
Take for example:
from element in _yourXDocument.Descendants("NewDataSet")
select element
where element.Descendants("M").Value = String.Empty;
For .Net 2.0, use XmlDocument.
精彩评论