Click here to Skip to main content
13,139,254 members (85,942 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi..
There is a table in which one column is in the form of an xml string,and this string contain another 5 column values.here the xml string is like child table of the first one..i want to access the attribute values in this xml string for checking a search criteria..Tell me please how can i access attributes..Please give me an example code to implement that..
Thanks in advance.
swathi.
Posted 21-Jan-13 17:37pm
Updated 21-Jan-13 19:26pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is my short overview of different XML parsing methods you can use:


  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
  Permalink  
Comments
A C swathi 22-Jan-13 0:45am
   
Thanks..can u please explain with an example.
   
I don't see why you need more explanation. No, first of all, did you already try any of these API? No? Then why wasting more time?

Try it out, and ask a question if you face any problems. Isn't it fair? After all, you are supposed to do you work be yourself, as you are a software developer.
—SA
   
OK, if you are a fresher, this is just yet another reason to start diffing into it. And then you will see how. Documentation is rich, with code samples... When I advise it, I know what I say. This is the most effective thing you can do about it. Any additional instructions won't help more, really.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

string str = "your xml string";
XDocument document = XDocument.Parse(str);
var books = document.Descendants("book");
foreach (XElement item in books)
{
    var idElement = item.Element("id");
    if (idElement.HasAttributes)
    {
        XAttribute attr = idElement.Attribute("isPrimaryKey");
        string attrValue = attr.Value;
        Console.WriteLine(attrValue);
    }
}
Console.Read();


XML:
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
	<id isPrimaryKey="true">3785</id>
        <name>CLR via C#</name>
</book>
</books>
  Permalink  
v4
Comments
A C swathi 23-Jan-13 2:27am
   
thanks..its working

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100