I need to validate as fast as possible and receive the next xml-data on socket.
I am using this method to validate received xml-datas.
private validateRecievedXmlCallback()
{
try
{
XmlReader xreader = XmlReader.Create(new StringReader(xmlData));
while (xreader.Read()) ;
}
catch (Exception)
{
return false;
}
return true;
}
But I think this method is not efficient enought. I actually need to check the last tag only.
example:
<test valueA="1" valueB="2">
<data valueC="1" />
<data valueC="5" />
<data valueC="5">220</data>
</test> //I need to check if </test> tag closed, but whats the best way to do it?
If you stick with the XmlReader, you could use XmlReader.Skip() which, well, skips the content of the current element.
So
As other commenters have stated already, there is no good way of guaranteeing well-formedness of a XML document except for using a XML parser.