Reputation: 1554
I have an XML file with syntax errors. eg.
<Viewport thisisbad Left="0" Top="0" Width="1280" Height="720" >
When i create an XML reader it does not throw any errors. I there a way to do syntax checking automatically, like XMLDocument does ?
I have tried setting various XmlReaderSettings flags but found nothing useful.
Upvotes: 4
Views: 3120
Reputation: 192437
To check whether an XML document is well-formed using an XmlReader, you must actually read the document.
In C#, this will do it:
var txt = "<Viewport thisisbad Left='0' Top='0' Width='1280' Height='720' >";
XmlReader reader = XmlReader.Create(new StringReader(txt));
while (reader.Read()) { }
The result I get from running that code is:
Exception: System.Xml.XmlException: 'Left' is an unexpected token. The expected token is '='. Line 1, position 21.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
at System.Xml.XmlTextReaderImpl.ThrowUnexpectedToken(String expectedToken1, String expectedToken2)
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
There's no need to manage a stack of elements, as suggested in another answer. XmlReader does it for you.
You wrote:
When i create an XML reader it does not throw any errors. I there a way to do syntax checking automatically, like XMLDocument does ?
The key thing to realize is that an XmlReader is an object that reads xml. If you just create it, it hasn't read any xml yet, so of course it cannot tell you whether the xml (which it has not read) is valid.
To check the syntax, or well-formed-ness of XML quickly, call Read() on the XmlReader until it returns null. It will do the checking for you. But, realize that once you havve done so, the XmlReader is at the end of the document. You need to reset in order to actually read and examine the content of the xml. Most applications I've seen do both concurrently. In other words, the application examines the content, and delegates the "syntax checking" as you put it, to the Reader:
XmlReader reader = XmlReader.Create(sr);
while (reader.Read()) // will throw if not well-formed
{
switch (reader.NodeType)
{
case XmlNodeType.XmlDeclaration:
...
break;
case XmlNodeType.Element:
...
break;
case XmlNodeType.Text:
...
break;
...
}
}
Upvotes: 2