tags:

views:

99

answers:

4

Hi all, 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?

+9  A: 

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;
dtb
You shouldn't need to cast to XmlElement - InnerText is defined in the parent XmlNode as virtual and XmlElement just overrides it like normal.
James Manning
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)
James Manning
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. :)
James Manning
A: 
XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
XmlNode x = doc.SelectSingleNode("/TimelineInfo/Ambassador");

x.InnerText will return the contents

J Angwenyi
A: 

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

czuroski
+1  A: 

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);
James Manning