Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate XML Syntax Only in C#

Tags:

c#

xml

There are a lot of tutorials that teach on how to validate XML against a schema. But now I want to validate XML syntax only, not against the schema. Meaning I just want to check whether the XML is well-form, that whether there are closing or opening tag that is not done properly.

Is there anyway I can do that in .Net?

like image 557
Graviton Avatar asked Jan 24 '23 11:01

Graviton


2 Answers

Or if you're on .NET 3.5, you can use XElement.Load().

LINQ to XML's loading functionality is built upon XmlReader. Therefore, you might catch any exceptions that are thrown by the XmlReader.Create overload methods and the XmlReader methods that read and parse the document.

like image 141
dahlbyk Avatar answered Jan 29 '23 13:01

dahlbyk


Just open it in an XmlReader and read to the end. If it makes it without throwing an exception, it's well formed.

like image 32
Joel Coehoorn Avatar answered Jan 29 '23 11:01

Joel Coehoorn