I am running this line of code, which executes perfect for roughly 800 of my records. It hits record 801, and I am thrown
An unhandled exception of type 'System.NullReferenceException' occurred in XMLPost.exe
Additional information: Object reference not set to an instance of an object.
This is the troublesome line of code, which I thought adding in the ?.Value
would account for null elements.
string sr12 = xml.Element("Parent").Element("Child1").Element("Child2").Element("Child3").Element("Child4").Element("Child5").Element("sr12")?.Value;
Console.WriteLine(sr12);
What should I alter for my variable sr12
so that it does not break and my code can continue to execute as expected?
EDIT
XML Structure
<parent>
<one>
<two>
<three>
<sr12></sr12>
</three>
</two>
</one>
<ten>
<eleven>
<twelve>
<thirteen>
<sr12></sr12>
</thirteen>
</twelve>
</eleven>
</ten>
</parent>
I have also tried this syntax
var result = (string) xml.Elements("Parent")
.Elements("ten")
.Elements("eleven")
.Elements("twelve")
.Elements("thirteen")
.Elements("homeaddress")
.SingleOrDefault();
Which produces an error of
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Core.dll
Additional information: Sequence contains more than one element