C # XmlElement: SelectSingleNode return null for empty string?

I am new to C # and just started using XmlElement and its SelectSingleNode method. My XML file has a tag that can have a value (i.e. <tag>value</tag> ) or be empty (i.e. <tag></tag> ). If it is empty, SelectSingleNode returns null.

I am currently using the following code to catch the value of a tag:

 XmlElement elem = .... string s = elem.SelectSingleNode("somepath").Value; 

This code obviously throws an exception for empty tags. However, for me, an empty tag is a valid value, where I expect the value of my string to be "".

Wrapping each SelectSingleNode call with try ... catch seems like a huge waste of code (I have many fields that may be empty), and I'm sure there is a better way to achieve this.

What is the recommended approach?

EDIT:

As requested, an example XML code would look like this:

 <Elements> <Element> <Name>Value</Name> <Type>Value</Type> <-- may be empty <Color>Value</Color> </Element> <Element> <Name>Value</Name> <Type>Value</Type> <Color>Value</Color> </Element> </Elements> 

CS Code:

 XmlDocument doc = new XmlDocument(); doc.Load("name.xml"); foreach (XmlElement elem in doc.SelectNodes("Elements/Element")) { myvalue = elem.SelectSingleNode("Type/text()").Value; } 
+6
c # xml
source share
5 answers

Your sample code:

  myvalue = elem.SelectSingleNode("Type/text()").Value; 

There is a problem. The XPath expression that you used there does not mean "give me the text of the Type element." This means "give me all the child text nodes of the type element." And the empty element has no child text nodes (node ​​text cannot be empty in the XPath document model). If you want to get the text value of node, you should use:

  myvalue = elem.SelectSingleNode("Type").InnerText; 
+10
source share

The recommended approach would be to use the .NET XML API (namely LINQ to XML).

Here is an example:

 using System; using System.Linq; using System.Xml.Linq; class Program { static void Main() { String xml = @"<Root><Value></Value></Root>"; var elements = XDocument.Parse(xml) .Descendants("Value") .Select(e => e.Value); } } 
+1
source share

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.value(VS.71).aspx

Since the return value of "value" depends on the type of NodeType, there is a possibility that node will be interpreted as a type that can return NULL.

You might be better off using:

 XmlElement elem = .... string s = elem.SelectSingleNode("somepath").InnerText; 

like XMLNode.InnerText (or XmlNode.InnerXML ) will return a string, including an empty string.

+1
source share

Maybe this will work for you:

 string s = elem.SelectSingleNode("somepath") != null ? elem.SelectSingleNode("somepath").value : "" 
0
source share

When I'm really worried about the XML DOM, you can write a helper method in the following lines:

 static string NodeValue(XmlNode node, string defaultValue) { if (node != null) return node.Value ?? defaultValue; return defaultValue; } 

Then you can do the following if you are not sure that your node will exist:

 string s = NodeValue(elem.SelectSingleNode("Type"), String.Empty); 

If your code is readable, especially if you are doing this for multiple elements.

All that said, SelectSingleNode (..) does not return a null value if the tag is empty. The Value attribute will be null. If you are just trying to get around this, this should do:

 string s = elem.SelectSingleNode("Type").Value ?? String.Empty; 

Edit: ah, you use / text () to select the actual text node. You could just get rid of this part of XPath, but the NodeValue method that I provided should still work (in this case, the "?? defaultValue" part is not needed).

0
source share

All Articles