-2

I want get <SNILS> node:

     <ArrayOfEmployee xmlns=""
     xmlns:i="http://www.w3.org/2001/XMLSchema-instance" version="2.0.11"
     formatVersion="2.0" system="ARM">  
 <Employee>
            <AdditionalLaborAgreement i:nil="true" />   <CertificateEducationList>
          <Document>    <SNILS>1111111111111</SNILS>
          </Document> 
 </CertificateEducationList>    
      </Employee>

Code:

 XmlNodeList nodes = xml.GetElementsByTagName("Employee");    
        XmlNode node = xml.SelectSingleNode("SVED_PR_GS/ZGLV/FILENAME");
        int Count = 2;
        foreach (XmlNode n in nodes)
        {
            XmlNode smr_vsi = n.SelectSingleNode("SNILS");
            Console.WriteLine(n.SelectSingleNode(smr_vsi.InnerText));
        }

Error: Console.WriteLine(n.SelectSingleNode(smr_vsi.InnerText));

The object reference does not point to an instance of the object.

  • Ok, and what is your question? What problem are you having? –  Dec 25 '17 at 12:08
  • "The object reference does not point to an instance of the object." – user8980331 Dec 25 '17 at 12:21
  • Possible duplicate of https://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean –  Dec 25 '17 at 12:21
  • 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) – Progman Dec 26 '17 at 11:40
  • What are you trying to read? The statement `n.SelectSingleNode(smr_vsi.InnerText)` in this case will look for an XML element `<1111111111111>...1111111111111>`, which doesn't make any sense. – Progman Dec 26 '17 at 11:42

1 Answers1

1

Your XML is malformed. Its missing </ArrayOfEmployee> element.

You can get the desired node in one of the 2 ways:

// Provide full XPath
XmlNode smr_vsi = n.SelectSingleNode("CertificateEducationList/Document/SNILS");

//Provide find on any path hint.
XmlNode smr_vsi = n.SelectSingleNode("//SNILS");

But, do check for null before using it:

if(smr_vsi != null)
   Console.WriteLine(smr_vsi.InnerText);
Sunil
  • 3,404
  • 10
  • 23
  • 31