David Dvali wrote:
I have such XML file:
<root>
<elem1>
<optional>
Some data
<optional>
^^^^^^^^^]
Needs to be </optional>
</elem1>
</root>
How can I get value of the "optional" node?
You can use XPath to find whether the <optional> element exists e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
XmlElement optional =
xmlDocument.SelectSingleNode(@"/root/elem1/optional") as XmlElement;
if (optional != null) {
Console.WriteLine("InnerText is {0}", optional.InnerText);
}
else {
Console.WriteLine("Element not found.");
}
Whether the InnerText gives you what you consider the "element value" I
don't know, lots of people have different opinions on that constitutes
the value of an element in an XML document.
For your above example the InnerText gives you the complete text
including white space that is in the <optional> element e.g. InnerText is
"
Some data
"
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/