How can I validate my xml?

J

Jonny

Hi,
I'm trying to validate my xml against a xsd but I can't get it to work.
Originally, I wanted to validate an xml string but since I didn't get that
to work I tried to validate an xml file instead. Didn't work either.
Actuallty, the xml gets loaded but there are no events raised that says my
xml is incorrect!

I'm using .NET 2.0 and my code is listed below. ALL help is appreciated!

Thanks!

/Jonny

void myValidate()
{
FileStream fs = File.Open(@"C:\Test.xml", FileMode.Open);

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null,@"C:\Test.xsd");
settings.ValidationType = ValidationType.Schema; //this line is
neccessary for validation to work
settings.ValidationEventHandler += new
ValidationEventHandler(rs_ValidationEventHandler);

XmlReader xreader = XmlReader.Create(fs, settings);

XmlDocument xdoc = new XmlDocument();
xdoc.Load(xreader);

fs.Close();
}

void rs_ValidationEventHandler(object sender, ValidationEventArgs e)
{
Debug.WriteLine("e.Message: " + e.Message);
}


Schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:bookstore-schema" elementFormDefault="qualified"
targetNamespace="urn:bookstore-schema">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:element name="comment" type="xsd:string" />
<xsd:element name="author" type="authorName"/>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
<xsd:element ref="comment" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element ref="author" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
</xsd:complexType>
</xsd:schema>

xml:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://myns/slidesdemo"
xmlns:rv="http://myns/slidesdemo/reviewdate">
<session name="All about XML">
<slide>
<slide position="1">
<title>Agenda</title>
<rv:reviewed>2004-05-10T00:00:00</rv:reviewed>
</slide>
<slide position="2">
<title>Introduction</title>
<rv:reviewed>2003-10-22T00:00:00</rv:reviewed>
</slide>
<ILLEGAL_NODE>
<NONSENSE>NONSENSE</NONSENSE>
</ILLEGAL_NODE>
<slide position="3">
<title>Code Examples</title>
<rv:reviewed>2004-03-02T00:00:00</rv:reviewed>
</slide>
</slide>
</session>
</root>
 
J

Jonny

Thanks for your response. Your first link shows an example using
XmlValidatingReader whixh I don't want to use since it's obsolete in 2.0.

The second example is good, but I can't get it to work. Using while (...)
doesn't change the behaviour for me.

/Jonny
 
J

Jonny

Thanks for your response.

The XmlValidatingReader is obsolete for 2.0 and therefor I want to find
another solution.

Thanks,
Jonny
 
M

Michael Nemtsev

Hello Jonny,

Try to use XmlValidatingReader for this

J> Hi,
J> I'm trying to validate my xml against a xsd but I can't get it to
J> work.
J> Originally, I wanted to validate an xml string but since I didn't get
J> that
J> to work I tried to validate an xml file instead. Didn't work either.
J> Actuallty, the xml gets loaded but there are no events raised that
J> says my
J> xml is incorrect!
J> I'm using .NET 2.0 and my code is listed below. ALL help is
J> appreciated!
J>
J> Thanks!
J>
J> /Jonny
J>
J> void myValidate()
J> {
J> FileStream fs = File.Open(@"C:\Test.xml", FileMode.Open);
J> XmlReaderSettings settings = new XmlReaderSettings();
J> settings.Schemas.Add(null,@"C:\Test.xsd");
J> settings.ValidationType = ValidationType.Schema; //this line is
J> neccessary for validation to work
J> settings.ValidationEventHandler += new
J> ValidationEventHandler(rs_ValidationEventHandler);
J> XmlReader xreader = XmlReader.Create(fs, settings);
J>
J> XmlDocument xdoc = new XmlDocument();
J> xdoc.Load(xreader);
J> fs.Close();
J> }
J> void rs_ValidationEventHandler(object sender, ValidationEventArgs e)
J> {
J> Debug.WriteLine("e.Message: " + e.Message);
J> }
J> Schema:
J> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
J> xmlns="urn:bookstore-schema" elementFormDefault="qualified"
J> targetNamespace="urn:bookstore-schema">
J> <xsd:element name="bookstore" type="bookstoreType" />
J> <xsd:element name="comment" type="xsd:string" />
J> <xsd:element name="author" type="authorName"/>
J> <xsd:complexType name="authorName">
J> <xsd:sequence>
J> <xsd:element name="first-name" type="xsd:string" />
J> <xsd:element name="last-name" type="xsd:string" />
J> </xsd:sequence>
J> </xsd:complexType>
J> <xsd:complexType name="bookstoreType">
J> <xsd:sequence maxOccurs="unbounded">
J> <xsd:element name="book" type="bookType" />
J> <xsd:element ref="comment" minOccurs="0" />
J> </xsd:sequence>
J> </xsd:complexType>
J> <xsd:complexType name="bookType">
J> <xsd:sequence>
J> <xsd:element name="title" type="xsd:string" />
J> <xsd:element ref="author" />
J> <xsd:element name="price" type="xsd:decimal" />
J> </xsd:sequence>
J> <xsd:attribute name="genre" type="xsd:string" />
J> </xsd:complexType>
J> </xsd:schema>
J> xml:
J> <?xml version="1.0" encoding="utf-8"?>
J> <root xmlns="http://myns/slidesdemo"
J> xmlns:rv="http://myns/slidesdemo/reviewdate">
J> <session name="All about XML">
J> <slide>
J> <slide position="1">
J> <title>Agenda</title>
J> <rv:reviewed>2004-05-10T00:00:00</rv:reviewed>
J> </slide>
J> <slide position="2">
J> <title>Introduction</title>
J> <rv:reviewed>2003-10-22T00:00:00</rv:reviewed>
J> </slide>
J> <ILLEGAL_NODE>
J> <NONSENSE>NONSENSE</NONSENSE>
J> </ILLEGAL_NODE>
J> <slide position="3">
J> <title>Code Examples</title>
J> <rv:reviewed>2004-03-02T00:00:00</rv:reviewed>
J> </slide>
J> </slide>
J> </session>
J> </root>
J>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
G

Guest

Hi,
I have used 2.0 it is working for me but I am not loading the XML thru
file, I am reading the XML string and validating with the XSD. here is the
code.

public bool Validate(string xmlStr, string xsdPath)
{
XmlTextReader textReader = getXmlReader(xmlStr);
return Validate(textReader, xsdPath);

}

public bool Validate(XmlTextReader nodeReader, string xsdPath)
{

XmlReader reader = null;
try
{
errors.Clear();
bValid = true;
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, xsdPath);

settings.ValidationType = ValidationType.Schema; //this line
is neccessary for validation to work
settings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
reader = XmlReader.Create(nodeReader, settings);

while (reader.Read()) ;
}
catch (XmlException xmle)
{
bValid = false;
errors.Add(xmle.Message);
//errMsg.Append(xmle.Message).Append("\n");
}
finally
{
//Close the reader.
nodeReader.Close();
reader.Close();
}

return bValid;


//return validationFlag;
}

------------------
private void ValidationCallBack(object sender, ValidationEventArgs e)
{
//Logger.Debug("Entering ValidationCallBack");
if (e.Severity == XmlSeverityType.Warning)
{
warnings.Add(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
bValid = false;
Console.WriteLine(e.Message);
errors.Add(e.Message);

}

//Logger.Debug("Exiting ValidationCallBack");
}
 

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

Top