Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Updated 21-Jan-13 19:26pm
v2

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
 
Share this answer
 
Comments
A C swathi 22-Jan-13 0:45am    
Thanks..can u please explain with an example.
Sergey Alexandrovich Kryukov 22-Jan-13 1:08am    
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
Sergey Alexandrovich Kryukov 22-Jan-13 1:48am    
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
C#
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
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
	<id isPrimaryKey="true">3785</id>
        <name>CLR via C#</name>
</book>
</books>
 
Share this answer
 
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)



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