Validating XML

M

Mahain

I have to validate xml file against xsd file using c#.
XmlReader only check for the well formated doucment but i want to
check for every thing like space,datatype and any extra text in xml
file.

pls help me do this

tell me some library if there is any to validate xml document.
 
M

Martin Honnen

Mahain said:
I have to validate xml file against xsd file using c#.
XmlReader only check for the well formated doucment but i want to
check for every thing like space,datatype and any extra text in xml
file.

Use an XmlReader with XmlReaderSettings set up for validation:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "schema.xsd");
bool valid = true;
settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs vargs)
{
if (vargs.Severity == XmlSeverityType.Error)
{
valid = false;
}
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};

using (XmlReader reader = XmlReader.Create(@"doc.xml", settings))
{
while (reader.Read()) {}
}
Console.WriteLine("Document is {0}.", valid ? "valid": "not valid");

See also the MSDN section:
<URL:http://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx>
 
M

Mahain

Use an XmlReader with XmlReaderSettings set up for validation:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "schema.xsd");
bool valid = true;
settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs vargs)
{
if (vargs.Severity == XmlSeverityType.Error)
{
valid = false;
}
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};

using (XmlReader reader = XmlReader.Create(@"doc.xml", settings))
{
while (reader.Read()) {}
}
Console.WriteLine("Document is {0}.", valid ? "valid": "not valid");

See also the MSDN section:
<URL:http://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx>

same result as XMLReader
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top