I get the data from the XML and want to write it into an array.

But I get an error:

Additional information: The object reference does not indicate an object instance.

The code itself:

XmlDocument xml = new XmlDocument(); xml.LoadXml(File.ReadAllText("company.xml")); XmlNodeList fullNames = xml.SelectNodes("/ΠšΠΎΠΌΠΌΠ΅Ρ€Ρ‡Π΅ΡΠΊΠ°ΡΠ˜Π½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡ/ΠžΡ€Π³Π°Π½ΠΈΠ·Π°Ρ†ΠΈΠΎΠ½Π½Π°ΡΠ‘Ρ‚Ρ€ΡƒΠΊΡ‚ΡƒΡ€Π°/Π Π°Π±ΠΎΡ‚Π½ΠΈΠΊΠΈ/Π Π°Π±ΠΎΡ‚Π½ΠΈΠΊ/НаимСнованиС"); XmlNodeList nodelogins = xml.SelectNodes("/ΠšΠΎΠΌΠΌΠ΅Ρ€Ρ‡Π΅ΡΠΊΠ°ΡΠ˜Π½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡ/ΠžΡ€Π³Π°Π½ΠΈΠ·Π°Ρ†ΠΈΠΎΠ½Π½Π°ΡΠ‘Ρ‚Ρ€ΡƒΠΊΡ‚ΡƒΡ€Π°/Π Π°Π±ΠΎΡ‚Π½ΠΈΠΊΠΈ/Π Π°Π±ΠΎΡ‚Π½ΠΈΠΊ/Π›ΠΎΠ³ΠΈΠ½"); // Π‘ΠΎΠ±ΠΈΡ€Π°ΡŽ Π»ΠΎΠ³ΠΈΠ½Ρ‹ ΠΈΠ· XML int i = 0; string[] loginArr = null; foreach (XmlNode nodelogin in nodelogins) { loginArr[i] = nodelogin.InnerText; i++; } MessageBox.Show(loginArr[0]); 

enter image description here

    2 answers 2

    You have not created a loginArr array. You have it is a null link. Therefore, you cannot use the indexing operator. Neither the memory for the array elements is allocated, nor the size of the array itself is unknown.

     string[] loginArr = null; foreach (XmlNode nodelogin in nodelogins) { loginArr[i] = nodelogin.InnerText; i++; ^^^^^^^^^^^ } 

    You first need to determine how many elements are in nodelogins and, based on this value, allocate memory for the elements of the loginArr array using the new operator.

      The array needs to be dimensioned.

       var loginArr = new string[nodelogins.Count]; 

      You can make through the list:

       var loginArr = new List<string>{}; foreach (XmlNode nodelogin in nodelogins) { loginArr.Add(nodelogin.InnerText); }