Click here to Skip to main content
12,558,332 members (61,524 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
There is a table in which one column is in the form of an xml string,and this string contain another 5 column 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.
Posted 21-Jan-13 17:37pm
Updated 21-Jan-13 19:26pm
Rate this: bad
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.

  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.

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.
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.
Rate this: bad
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;

<?xml version="1.0" encoding="utf-8"?>
	<id isPrimaryKey="true">3785</id>
        <name>CLR via C#</name>
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 | Mobile
Web01 | 2.8.161026.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2016
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