Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

How do you determine if an attribute exists in a XML node without a Null Reference error?

$
0
0

If the attribute "name" is not in the node, I assumed the following would work:

 

 if (objTmplt.Attributes.GetNamedItem("name").Value != null)

styleName = objTmplt.Attributes[Atrb.Name].Value;

 

However, it throws an unhandled exception. This has me backed into a corner where trying to prevent a null unhandled exception causes an exception. I worked with both methods of Attributes.GetNamdeItem and Attributes["name"] with the same effect.

 

I want to avoid wrapping hundreds of lines with try-catch or iteration blocks. In VB.Net the Attributes.GetNamedItem does not throw an error if the attribute is missing. It simply creates an empty string. C# seems to have a different result.

 

It is late and I am sure I am just missing something.

 

 

 

 

 

 

 

 


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>