Manipulating xml with c# using xpath, a little help needed please

Posted on 2006-06-01
Last Modified: 2010-04-16
Hi there,

I have an xml file stored as a string in a variable.... it looks exactly like this

<t id="11" s="2" /><t id="1" s="5." />

currently there is 2 records but there will be many more..

I need to extract the contents of id and s... i.e. first record id=11 and s=2

I got so far ... just need a little help with the expression i think

                            XPathDocument document = new XPathDocument(xmlFields);  // CAN I DO THIS WHEN ITS A STRING?
                            XPathNavigator navigator = document.CreateNavigator();
                            XPathNavigator expr = navigator.Compile("/t id); // ???????????

                            XPathNodeIterator nodes = navigator.Select(expr);

                            while (nodes.MoveNext())
                                       // need to add each value to a local variable
Question by:ianinspain
    LVL 96

    Expert Comment

    by:Bob Learned
    Try this:

    XmlDocument doc = new XmlDocument();

    foreach (XmlNode node in doc.SelectNodes("//t"))
       string id = node.Attributes["id"].value;

    LVL 2

    Accepted Solution

    Try this:

    // String from the xml file
    String xml = "<root><t id='11' s='2'/><t id='1' s='5'/></root>";

    StringReader xmlFields = new StringReader(xml); // you can do this if u have a root tag

    XPathDocument document = new XPathDocument(xmlFields);
    XPathNavigator navigator = document.CreateNavigator();

    // Expressions for both id and s
    XPathExpression expr1 = navigator.Compile("/root/t/@id");
    XPathExpression expr2 = navigator.Compile("/root/t/@s");
    // Create iterators
    XPathNodeIterator nodes1 = navigator.Select(expr1);
    XPathNodeIterator nodes2 = navigator.Select(expr2);      
    // Prints both the value i.e. id and s
    while (nodes1.MoveNext() && nodes2.MoveNext())
          Console.WriteLine("id = " + nodes1.Current + " :s = " + nodes2.Current);

    Anandavally P      

    Author Comment

    thanks ... that worked great


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now