Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)

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

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

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

You can use the XmlDocument[^]class and XPath[^] to find values, or you can use Linq to XML[^].
Share this answer
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

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.
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.

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