Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Validating XML

1 view
Skip to first unread message

Mahain

unread,
May 8, 2008, 6:43:30 AM5/8/08
to
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.

Martin Honnen

unread,
May 8, 2008, 6:55:33 AM5/8/08
to
Mahain wrote:
> 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>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Peter Morris

unread,
May 8, 2008, 8:22:30 AM5/8/08
to

Mahain

unread,
May 9, 2008, 1:15:38 AM5/9/08
to

same result as XMLReader

0 new messages