Getting data from xml by attribute value C# using XmlTextReader

爷,独闯天下 提交于 2020-01-06 07:46:07

问题


I have a file with the following content (myfile.xml). I have to get all content coming under (including product node) a product with id=1.

<products>  
  <product id="1">
       <category>q</category>  
  </product>    
  <product id="2">      
       <category>w</category>    
  </product>   
  <product id="3">       
  <category>e</category>   
 </product>
</products>`

i.e. the result should be :

 <product id="1"> 
      <category>q</category>
  </product> 

How can I do this?

I have the restriction that I should use XmlTextReader or XPathNavigator only for this, otherwise I could have used this : Getting data from xml by attribute vlaue


回答1:


Wouldn't XmlReader.GetAttribute() work?

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.getattribute(v=vs.100)

Add in some Skip() to move the iterator:

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.skip(v=vs.100)



来源:https://stackoverflow.com/questions/10680553/getting-data-from-xml-by-attribute-value-c-sharp-using-xmltextreader

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!