开发者

Trimming the XML file

开发者 https://www.devze.com 2022-12-25 15:56 出处:网络
I have a XML file as shown below <NewDataSet> - <T> <P /> <C>1</C> <M />

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消