0

Trying to get orderCount and completedCount from my XML file:

<?xml version="1.0" encoding="utf-8"?>
<MENU xmlns="http://test.xsd">
    <STATUS>
        <ORDER_COUNT>22</ORDER_COUNT>
        <COMPLETED_COUNT>0</COMPLETED_COUNT>
        </STATUS>
    <ITEM>
        <ITEM1>BURGER</ITEM1>
    </ITEM>
</MENU>

With my C# code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlerrorFile);
XmlNamespaceManager xmlNS = new XmlNamespaceManager(xmlDoc.NameTable);
xmlNS.AddNamespace("ns", @"http://test.xsd");
XmlNodeList nl = xmlDoc.SelectNodes("ns:MENU/ns:STATUS", xmlNS);

      foreach (XmlNode xndNode in nl)
            {
                string orderCount = xndNode.SelectSingleNode("ORDER_COUNT").InnerText;
                string completedCount = xndNode.SelectSingleNode("COMPLETED_COUNT").InnerText;

                if (orderCount != "0")
                {
                    label1.Text = "Error";
                }

                else
                {
                    label1.Text = "Good";
                }


             }  

Im am getting the error, "Object reference not set to an instance of an object".

for orderCount and completedCount.

Jerry Trac
  • 329
  • 3
  • 15
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 28 '19 at 17:23

1 Answers1

0

using debugger check "nl" not null,after it check "xndNode" into foreach not null

surferNet
  • 39
  • 1