Link to home
Start Free TrialLog in
Avatar of ianinspain
ianinspain

asked on

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

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
                            }
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Try this:

XmlDocument doc = new XmlDocument();
doc.Load(filePath);

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

Bob
ASKER CERTIFIED SOLUTION
Avatar of Anandavally_P
Anandavally_P

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ianinspain
ianinspain

ASKER

thanks ... that worked great