[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2006-06-01
Medium Priority
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
ID: 16806268
Try this:

XmlDocument doc = new XmlDocument();

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


Accepted Solution

Anandavally_P earned 2000 total points
ID: 16806699
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

ID: 16832409
thanks ... that worked great


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 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