Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,all

does anyone know how to retrieve xml attribute value in c#.
XML
- <Site>
- <Properties>
  <Property id="site.version" value="1.0" />
  <Property id="movex.version" value="10.0" />
  <Property id="webservice.date.format" value="yyyyMMdd" />
  <Property id="display.format" value="dd-MM-yy" />
  </Properties>



Take a look at the xml file above, how can i retrieve the property value where

id == movex.version. I would rather not using index , i prefer to get the movex.version value

from selecting where id=movex.version or something

any help would be really appreciated


thanks
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jun-13 21:20pm    
What is the question, "does anyone know"? Very nice. Many know. Is it helpful?
—SA

You can use the XmlDocument[^]class and XPath[^] to find values, or you can use Linq to XML[^].
 
Share this answer
 
Comments
codingStar 18-Jun-13 21:18pm    
hi,christian, i know the general idea but just can't know exactly how to achieve this since it got many value and id. And I don't want to use the index to get the value.

can you provide me some code example to achieve this

thanks
Christian Graus 18-Jun-13 21:19pm    
Read the links I gave you. Linq to XML creates class instances with named attributes. XPath lets you look up attributes by name.
There are different ways of XML parsing provided in .NET FCL. This is my short overview of them:


  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900