dotnet xml:
I am wondering if I am alone with this problem. Using VS 2005, I must
validate an XML file via a Schema and it works well. When I get the
schema exception and check the LineNumber and LinePosition properties,
they are always set to 0 <===
Here are 2 ways I used to validate the XML always getting LineNumber
and LinePosition = 0.
Any hints would be greatly appreciated.
==================================================================
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = false;
settings.Schemas.Add(Schema);
settings.ValidationType = ValidationType.Schema;
FileStream stXml = new FileStream(sXmlFile,
FileMode.Open);
XmlReader xrdr = XmlReader.Create(stXml, settings);
try
{
while (xrdr.Read());
}
catch (XmlException ex)
{
sMessage = "XML Format error in Xml file: " +
sXmlFile + " ===> " + ex.Message;
return false;
}
catch (XmlSchemaValidationException ex)
{
sMessage = "Error in Xml file: " + sXmlFile + " at
line " +
ex.LineNumber + ", column " + ex.LinePosition +
": " + ex.Message;
return false;
string s = ex.Message + " (line " +
settings.LineNumberOffset + "." + settings.LinePositionOffset + ")";
}
xrdr.Close();
stXml.Close();
=================================================================
XmlDocument Xml = new XmlDocument();
Xml.Schemas.Add(Schema);
Xml.PreserveWhitespace = true;
try
{
Xml.Load(sXmlFile);
Xml.Validate(null);
}
catch (XmlException ex)
{
sMessage = "XML Format error in Xml file: " +
sXmlFile + " ===> " + ex.Message;
return false;
}
catch (XmlSchemaValidationException ex)
{
sMessage = "Error in Xml file: " + sXmlFile + " at
line " +
ex.LineNumber + ", column " + ex.LinePosition +
": " + ex.Message;
return false;
}