12

simple question but I've been dinking around with it for an hour and it's really starting to frustrate me. I have XML that looks like this:

  <TimelineInfo>
    <PreTrialEd>Not Started</PreTrialEd>
    <Ambassador>Problem</Ambassador>
    <PsychEval>Completed</PsychEval>
  </TimelineInfo>

And all I want to do is use C# to get the string stored between <Ambassador> and </Ambassador>.

So far I have:

XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
XmlNode x = doc.SelectSingleNode("/TimelineInfo/Ambassador");

which selects the note just fine, now how in the world do I get the content in there?

4 Answers 4

17

May I suggest having a look at LINQ-to-XML (System.Xml.Linq)?

var doc = XDocument.Load("C:\\test.xml");

string result = (string)doc.Root.Element("Ambassador");

LINQ-to-XML is much more friendly than the Xml* classes (System.Xml).


Otherwise you should be able to get the value of the element by retrieving the InnerText property.

string result = x.InnerText;
Sign up to request clarification or add additional context in comments.

3 Comments

You shouldn't need to cast to XmlElement - InnerText is defined in the parent XmlNode as virtual and XmlElement just overrides it like normal.
BTW, in case others run across this - the cast to string is important (XElement defines lots of explicit conversions so you can do stuff like this and not have to call Convert methods yourself) - you might be tempted to ToString instead, but that doesn't get you the same result (it would get the entire element, including opening, closing, and content)
BTW, since in this example snippet we're not really using the XDocument, a slightly simpler version would be XElement.Load the file which gets you the root element directly. However, that's just a point of trivia - XDocument.Load is certainly what you want to use in the typical case. :)
4

The InnerText property should work fine for you.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.innertext.aspx

FWIW, you might consider switching API to linq-to-xml (XElement and friends) as IMHO it's a friendly, easier API to interact with.

System.Xml version (NOTE: no casting to XmlElement needed)

var xml = @"<TimelineInfo>
                <PreTrialEd>Not Started</PreTrialEd>
                <Ambassador>Problem</Ambassador>
                <PsychEval>Completed</PsychEval>
            </TimelineInfo>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var node = doc.SelectSingleNode("/TimelineInfo/Ambassador");
Console.WriteLine(node.InnerText);

linq-to-xml version:

var xml = @"<TimelineInfo>
                <PreTrialEd>Not Started</PreTrialEd>
                <Ambassador>Problem</Ambassador>
                <PsychEval>Completed</PsychEval>
            </TimelineInfo>";
var root = XElement.Parse(xml);
string ambassador = (string)root.Element("Ambassador");
Console.WriteLine(ambassador);

Comments

3
XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
XmlNode x = doc.SelectSingleNode("/TimelineInfo/Ambassador");

x.InnerText will return the contents

1 Comment

Simple and efficient. Kudos
0

Try using Linq to XML - it provides a very easy way to query xml datasources - http://msdn.microsoft.com/en-us/library/bb387098%28v=VS.100%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.