I would like to read a processing instruction from some small well-formed XML chunk, based on this post: How to read processing instruction from an XML file using .NET 3.5
However, it doesn't work. I get the error, that the pi object is null. This is what I do:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>Text <?pi 1?>blabla</root>");
Console.WriteLine(doc.ChildNodes[0].Name); // output: root
XmlProcessingInstruction pi = doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "pi").FirstOrDefault();
Console.WriteLine(pi.Value);
Parsing the XML works. When I get the error (System-NullReferenceException) in Visual Studio, I get it for line "Console.WriteLine(pi.Value);".
Where the error? How do I get/read the processing instruction?