Check wellformedness of XML

  • Thread starter Thread starter Tom S
  • Start date Start date
T

Tom S

Is there a way (without a schema or dtd) to check that my XML (currently
held in a string variable) is wellformed? I've looked on google and in the
help files and couldn't find anything about this.
Thanks!

Tom S
 
Hi Tom,

You can read the XML with XmlTextReader. To quote MSDN:

"The XmlTextReader class is an implementation of XmlReader, and provides a
fast, performant parser. It enforces the rules that XML must be well-formed.
It is neither a validating nor a non-validating parser since it does not
have DTD or schema information. It can read text in blocks, or read
characters from a stream."

And an example from another MSDN article to which I have added code to read
the whole contents of the XML stream.

string str = "<ROOT>AQID</ROOT>";
XmlTextReader r = new XmlTextReader(new StringReader(str));
try
{
while (r.Read())
{
}
}
finally
{
r.Close();
}
 
Great, thanks!

Dmitriy Lapshin said:
Hi Tom,

You can read the XML with XmlTextReader. To quote MSDN:

"The XmlTextReader class is an implementation of XmlReader, and provides a
fast, performant parser. It enforces the rules that XML must be
well-formed. It is neither a validating nor a non-validating parser since
it does not have DTD or schema information. It can read text in blocks, or
read characters from a stream."

And an example from another MSDN article to which I have added code to
read the whole contents of the XML stream.

string str = "<ROOT>AQID</ROOT>";
XmlTextReader r = new XmlTextReader(new StringReader(str));
try
{
while (r.Read())
{
}
}
finally
{
r.Close();
}

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Tom S said:
Is there a way (without a schema or dtd) to check that my XML (currently
held in a string variable) is wellformed? I've looked on google and in
the help files and couldn't find anything about this.
Thanks!

Tom S
 
Back
Top