xml - C# XmlElement: SelectSingleNode returns null for empty string? -


I'm new to C #, and just started using xml aliment and its ceslingnod method. My XML file contains a tag that can have a value (i.e. & lt; tag & gt; value & lt; / tag & gt; ) or empty (i.e. & lt; Tag & gt; & lt; tag & gt; ). If it is empty, select Sync Faucet Returns.

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

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

This code clearly raises an exception for empty tags. However, an empty tag for me is a valid value, where I should have the value of my string "." "

Try to select the SelectSingleNode to wrap each call ... a big waste of code hold (looks like I have many fields that can be empty), and I believe that to get it There is a better way.

What is the recommended approach?

Edit:

After the requests, there will be a sample XML code:

< Pre> element> gt; element & gt; & lt; name & gt; value & lt; / name & gt; & lt; type & gt; value & lt; / type & gt; ; & Lt; - Can be empty & lt; r C & gt; value & lt; / color & gt; & lt; / element & gt; & lt; element & gt; & lt; name & gt; value & lt; / name & gt; & lt; type & gt; ; Value & lt; / type & gt; & lt; color & gt; value & lt; / color & gt; & lt; / element & gt; & lt; / elements & gt;

CS code:

  XmlDocument doc = New XmlDocument (); doc.Load ("name.xml"); (Doctor in XMLAML. Select Nodes ("Elements / Elements") } {Myvalue = elem.SelectSingleNode ("type / text ()"). Value;}  

Your sample code:

  myvalue = elem.SelectSingleNode ("type / text ()") value.  

, where the problem is that the Expath expression you used does not mean that "Let me text the element type ". It means "give me all the text types of text nodes" and an empty element has no child text nodes (a text node can not be empty in the XPath document model). If you want to get the text value of the node, then you should use it:

  myvalue = elem.SelectSingleNode ("type"). Intertext;  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -