Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : XmlValidatingReader unbelievable error


amaretos NO[at]SPAM yahoo.com
7/13/2004 8:25:53 AM
Hi everyone, I am trying now for 4-5 hours to write a simple code to
validate a very simple XML and i still get an error. Here is my Code,
and i always get an error at args.Exception

("The 'urn:books:catalog' element is not declared. An error occurred
at file:///c:/inetpub/wwwroot/LakisService/books.xml, (2, 2).")

private void Button2_Click(object sender, System.EventArgs e) {
XmlTextReader r = new XmlTextReader(Server.MapPath("books.xml"));
XmlSchema xs = new XmlSchema();
xs.SourceUri = Server.MapPath("books.xsd");

XmlValidatingReader v = new XmlValidatingReader(r);
v.Schemas.Add(xs);
v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler += new ValidationEventHandler(MyValidation);
while (v.Read()){
}
v.Close();
}

public static void MyValidation(object sender, ValidationEventArgs
args){
string strErr="";
if (args.Exception != null){
strErr = args.Message;
}
}

HERE IS XML FILE
--------------
<?xml version="1.0" encoding="utf-8" ?>
<x:catalog xmlns:x="urn:books">
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with
XML.</description>
</book>
</x:catalog>

HERE IS XSD FILE
---------------
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="author" type="xsd:string" />
<xsd:element name="title" type="xsd:string" />
<xsd:element name="genre" type="xsd:string" />
<xsd:element name="price" type="xsd:float" />
<xsd:element name="publish_date" type="xsd:date" />
<xsd:element name="description" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>


Please if anyone knows help!!

Martin Honnen
7/13/2004 5:37:15 PM


[quoted text, click to view]


[quoted text, click to view]

Well you need to define the element in the right namespace, in your
schema it has no namespace but in the XML instance it has a namespace.
However usually you need to have one schema per namespace.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Oleg Tkachenko [MVP]
7/13/2004 6:34:00 PM
[quoted text, click to view]

Well, you schema doesn't define catalog element in "urn:books"
namespace. Instead it defines catalog element in no namespace.
So either remove namespace from catalog element in your XML or change
the schema.
--
Oleg Tkachenko [XML MVP]
Chris Lovett
7/15/2004 8:38:52 AM
Change your schema to the following:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:books" targetNamespace="urn:books">


[quoted text, click to view]

AddThis Social Bookmark Button