471,353 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Validating XML

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.
Jun 27 '08 #1
3 1570
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/
Jun 27 '08 #2
On May 8, 3:55 pm, Martin Honnen <mahotr...@yahoo.dewrote:
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/
same result as XMLReader
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris Dunaway | last post: by
reply views Thread by Gary Shell | last post: by
4 posts views Thread by easoftware | last post: by
3 posts views Thread by TheSteph | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.