Click here to Skip to main content
Sign Up to vote bad
good
See more: C#WPF
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:37
Edited 21 Jan '13 - 19:26


2 solutions

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.  
  3. 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[^].
  4.  
  5. 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:45
Thanks..can u please explain with an example.
Sergey Alexandrovich Kryukov - 22 Jan '13 - 1:08
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:48
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
            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  
Comments
A C swathi - 23 Jan '13 - 2:27
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 218
1 Sergey Alexandrovich Kryukov 159
2 Santhosh G_ 155
3 Richard MacCutchan 145
4 Maciej Los 136
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,937
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,135


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid