By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,589 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,589 IT Pros & Developers. It's quick & easy.

How can I validate my xml?

P: n/a
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>
May 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
take a look here
http://www.codeproject.com/soap/Simp..._Validator.asp

and here
http://msdn2.microsoft.com/en-us/8f0h7att.aspx

my opinio is tha you are not use while (reader.Read()); as shown in
example
hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 29 '06 #2

P: n/a
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
"Galcho[MCSD.NET]" <ga****@gmail.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
take a look here
http://www.codeproject.com/soap/Simp..._Validator.asp

and here
http://msdn2.microsoft.com/en-us/8f0h7att.aspx

my opinio is tha you are not use while (reader.Read()); as shown in
example
hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 30 '06 #3

P: n/a
Thanks for your response.

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

Thanks,
Jonny

"Michael Nemtsev" <ne*****@msn.com> wrote in message
news:9c**************************@msnews.microsoft .com...
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

May 30 '06 #4

P: n/a
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
May 31 '06 #5

P: n/a
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");
}
-----------------------------------------

But another problem with this is it is not accumalting all the errors, once
it encounters the validation error it is coming out, as per my understanding
it shud display all the errors in the XML file
"Jonny" wrote:
Thanks for your response.

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

Thanks,
Jonny

"Michael Nemtsev" <ne*****@msn.comwrote in message
news:9c**************************@msnews.microsoft .com...
Hello Jonny,

Try to use XmlValidatingReader for this
JHi,
JI'm trying to validate my xml against a xsd but I can't get it to
Jwork.
JOriginally, I wanted to validate an xml string but since I didn't get
Jthat
Jto work I tried to validate an xml file instead. Didn't work either.
JActuallty, the xml gets loaded but there are no events raised that
Jsays my
Jxml is incorrect!
JI'm using .NET 2.0 and my code is listed below. ALL help is
Jappreciated!
JJThanks!
JJ/Jonny
JJvoid myValidate()
J{
JFileStream fs = File.Open(@"C:\Test.xml", FileMode.Open);
JXmlReaderSettings settings = new XmlReaderSettings();
Jsettings.Schemas.Add(null,@"C:\Test.xsd");
Jsettings.ValidationType = ValidationType.Schema; //this line is
Jneccessary for validation to work
Jsettings.ValidationEventHandler += new
JValidationEventHandler(rs_ValidationEventHandler) ;
JXmlReader xreader = XmlReader.Create(fs, settings);
JJXmlDocument xdoc = new XmlDocument();
Jxdoc.Load(xreader);
Jfs.Close();
J}
Jvoid rs_ValidationEventHandler(object sender, ValidationEventArgs e)
J{
JDebug.WriteLine("e.Message: " + e.Message);
J}
JSchema:
J<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Jxmlns="urn:bookstore-schema" elementFormDefault="qualified"
JtargetNamespace="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>
Jxml:
J<?xml version="1.0" encoding="utf-8"?>
J<root xmlns="http://myns/slidesdemo"
Jxmlns: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


Aug 24 '06 #6

P: n/a
Do you have your example of "getXmlReader(xmlStr)" ?

Thanks,
Eric.-
"Kaladhar" wrote:
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");
}
-----------------------------------------

But another problem with this is it is not accumalting all the errors, once
it encounters the validation error it is coming out, as per my understanding
it shud display all the errors in the XML file
"Jonny" wrote:
Thanks for your response.

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

Thanks,
Jonny

"Michael Nemtsev" <ne*****@msn.comwrote in message
news:9c**************************@msnews.microsoft .com...
Hello Jonny,
>
Try to use XmlValidatingReader for this
JHi,
JI'm trying to validate my xml against a xsd but I can't get it to
Jwork.
JOriginally, I wanted to validate an xml string but since I didn't get
Jthat
Jto work I tried to validate an xml file instead. Didn't work either.
JActuallty, the xml gets loaded but there are no events raised that
Jsays my
Jxml is incorrect!
JI'm using .NET 2.0 and my code is listed below. ALL help is
Jappreciated!
JJThanks!
JJ/Jonny
JJvoid myValidate()
J{
JFileStream fs = File.Open(@"C:\Test.xml", FileMode.Open);
JXmlReaderSettings settings = new XmlReaderSettings();
Jsettings.Schemas.Add(null,@"C:\Test.xsd");
Jsettings.ValidationType = ValidationType.Schema; //this line is
Jneccessary for validation to work
Jsettings.ValidationEventHandler += new
JValidationEventHandler(rs_ValidationEventHandler) ;
JXmlReader xreader = XmlReader.Create(fs, settings);
JJXmlDocument xdoc = new XmlDocument();
Jxdoc.Load(xreader);
Jfs.Close();
J}
Jvoid rs_ValidationEventHandler(object sender, ValidationEventArgs e)
J{
JDebug.WriteLine("e.Message: " + e.Message);
J}
JSchema:
J<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Jxmlns="urn:bookstore-schema" elementFormDefault="qualified"
JtargetNamespace="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>
Jxml:
J<?xml version="1.0" encoding="utf-8"?>
J<root xmlns="http://myns/slidesdemo"
Jxmlns: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
>
>
Oct 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.