I am writing xml with XmlWriter . There are many sections in my code:
xml.WriteStartElement("payload"); ThirdPartyLibrary.Serialise(results, xml); xml.WriteEndElement();
The problem is that the ThirdPartyLibrary.Serialise method ThirdPartyLibrary.Serialise unreliable. It may happen (depending on the results variable) that it does not close all the tags that it opens. As a result, my WriteEndElement line is perverted, consumes closing the library widget tags, and doesnโt write </payload> .
Thus, I would like to make a verified call to WriteEndElement, which checks the name of the element and throws an exception if the cursor is not in the expected element.
xml.WriteEndElement("payload");
You can think of it as XmlReader.ReadStartElement(name) , which returns if the cursor is not at the expected location in the document.
How can I achieve this?
Edit: The second use case for this extension method is to make my own code more readable and reliable.
Colonel panic
source share