XML validation unknown errors?

P

Piotrekk

Hi

I have the following XML document and corresponding XSD schema.

<?xml version="1.0" encoding="utf-8"?>
<A>
<B></B>
<C>
<A/>
</C>
</A>


---------------------------


<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/
XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://
www.w3.org/2001/XMLSchema">

<xs:complexType name="A">
<xs:sequence>
<xs:element nillable="true" minOccurs="0" maxOccurs="1" name="B"
type="B" />
<xs:element nillable="true" minOccurs="0" maxOccurs="1" name="C"
type="C" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="B">
<xs:sequence>
<xs:element nillable="true" minOccurs="0" maxOccurs="1" name="A"
type="A" />
<xs:element nillable="true" minOccurs="0" maxOccurs="1" name="C"
type="C" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="C">
<xs:sequence>
<xs:element nillable="true" minOccurs="0" maxOccurs="1" name="A"
type="A" />
</xs:sequence>
</xs:complexType>
<xs:element name="A" type="A"/>
</xs:schema>


------------------------------------------------------------


When i test the XML against the schema bu using the following code, I
am getting:

Error :Could not find schema information for the element 'A'.
Error :Could not find schema information for the element 'B'.
Error :Could not find schema information for the element 'C'.
Error :Could not find schema information for the element 'A'.
All ok





CODE:


static void Main(string[] args)
{
XmlTextReader r = new XmlTextReader("doc.xml");
XmlValidatingReader validator = new
XmlValidatingReader(r);
validator.ValidationType = ValidationType.Schema;

XmlSchemaCollection schemas = new XmlSchemaCollection();
schemas.Add(null, "schema.xsd");

validator.Schemas.Add(schemas);
validator.ValidationEventHandler += new
ValidationEventHandler(validator_ValidationEventHandler);

try
{
while (validator.Read()) { }
}
catch (XmlException err)
{
Console.WriteLine("Validation error");
Console.WriteLine(err.Message);
}
finally
{
validator.Close();
}

Console.WriteLine("All ok");
}

static void validator_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
Console.WriteLine("Error :" + e.Message);
}



What does the error mean?
Best Regards
Piotr Kolodziej
 
M

Martin Honnen

Piotrekk said:
I have the following XML document and corresponding XSD schema.

<?xml version="1.0" encoding="utf-8"?>
<A>
<B></B>
<C>
<A/>
</C>
</A>

The elements in the example XML document are in no namespace while your
schema
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/
XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://
www.w3.org/2001/XMLSchema">

defines elements in its target namespace
http://tempuri.org/XMLSchema.xsd. So depending on what you want you
either need to change the example XML document to put the elements in
that namespace e.g.
<A xmlns="http://tempuri.org/XMLSchema.xsd">
...
</A>
or you need to change the schema and remove the targetNamespace attribute.
 

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