1

I am scrolling thorugh my XML and having hit a RoadBlock. My issue is i have a XML doc of a product which has several features. So i traverse throught the features in this way.

 XmlNodeList nodelist = XMLdoc.SelectNodes("//ProductFeature");
    foreach (XmlNode singlenode in nodelist)
    {
       Presentation_Value = singlenode.Attributes["Presentation_Value"].InnerText;
    }

Now the single node has this XML for all the nodes except one.

  <ProductFeature Localized="0" ID="149991561" Local_ID="0" Value="8 - 80" CategoryFeature_ID="12224" CategoryFeatureGroup_ID="2698" No="100033" Presentation_Value="8 - 80%" Translated="0" Mandatory="0" Searchable="0">

The last node has this XML

<ProductFeature Localized="0" ID="149812647" Local_ID="0" Value="abc.biz" CategoryFeature_ID="33538" CategoryFeatureGroup_ID="10889" No="0" Translated="1" Mandatory="0" Searchable="0">

Since the Last node does not have the attribute "Presentation_Value" I get the Infamous error below

Object reference not set to an instance of an object.

How can i resolve this?

MarsOne
  • 2,035
  • 4
  • 25
  • 48
  • Um, don't use the `InnerText` property unconditionally? Basically this is the same as any other NRE question... (I'd personally encourage you to use LINQ to XML anyway, which makes this simpler as you can just cast the attribute to `string` and you'll end up with null if the attribute wasn't found.) – Jon Skeet Jul 15 '14 at 11:00

0 Answers0