Hi !
I have problem with validating of the document with multiple
namespaces. The odd thing is, that my data work O'K when I test it
under XMLSpy but it doesn't work with my C# code.
My first 'main' schema is as follows:
incr2.xsd
----------
<xs:schema targetNamespace ="http://vit.volvo.com/dept9272/2004/ESB"
xmlns:p="tnsper son" xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied">
<xs:import namespace="tnsp erson" schemaLocation= "incr11.xsd "/>
<xs:element name="record">
<xs:complexTy pe >
<xs:sequence>
<xs:element ref="p:person"/>
<xs:element name="info" type="xs:string "/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
As you can see, it imports another schema,
incr11.xsd
----------
<xs:schema targetNamespace ="tnsperson" xmlns:t = "tnsperson"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexTy pe name="persontyp e">
<xs:sequence>
<xs:element name="firstname " type="xs:string "/>
<xs:element name="lastname" type="xs:string "/>
<xs:element name="age" type="xs:string "/>
</xs:sequence>
</xs:complexType>
<xs:element name="person" type="t:persont ype">
</xs:element>
</xs:schema>
The sample document is here:
sample.xml
-----------------
<?xml version="1.0" encoding="UTF-8"?>
<record xmlns="http://vit.volvo.com/dept9272/2004/ESB"
xmlns:p="tnsper son" >
<person xmlns="tnsperso n">
<firstname>To m</firstname>
<lastname>Rom </lastname>
<age>22</age>
</person>
<info>x_info</info>
</record>
The error message is as follows:
*************** **
The element 'tnsperson:pers on' has invalid child element
'tnsperson:firs tname'. Expected 'firstname'. An error occurred at file
sample.xml, (4, 4).
*************** **
I make the validation using the code presented below. The 'filename' is
XML doc filename, 'SchemaURL' xsd filename and TargetNamespace has
http://vit.volvo.com/dept9272/2004/ESB as a value.
reader = new XmlTextReader(f ilename);
reader.Whitespa ceHandling=Whit espaceHandling. None;
// ^^-- to avoid problems when validating indented docs
XmlSchemaCollec tion xsc = null;
xsc = new XmlSchemaCollec tion();
xsc.Add(TargetN amespace, SchemaURL);
vreader = new XmlValidatingRe ader(reader);
vreader.Schemas .Add(xsc);
vreader.Validat ionEventHandler += new
ValidationEvent Handler(this.Va lidationCallbac k);
while (vreader.Read() )
{}
Why the parser expects child element from default namespace, when its
parent is declared as a reference from other namespace ?
Thanks in advance for any help. I'e spent two days browsing and
testing. No luck yet :-(