I am trying to validate an xml file and schema and when I
am trying to validate I am getting the following error. I
have been trying to come out with a solution but I have
failed so far. The document validates ok in xmlSpy but it
fails in VS2003. Is there any article or fix for this?
Thnaks in advance.
Error:
======
"The Keyref 'Details' cannot find referred key or unique
in scope. An error occurred at file:///C:/test1.xml, (7,
4)..\n"
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Renewals>
<Renewal>
<Headers>
<e1>String</e1>
</Headers>
<Details>
<e1>String</e1>
<e2>0</e2>
</Details>
</Renewal>
</Renewals>
XSD file:
=========
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 4 U
(http://www.xmlspy.com) by Tomas Rivas (Tyco Healthcare-
Kendall) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Renewals">
<xs:annotation>
<xs:documentation>Renewal
document</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element
name="Renewal">
<xs:complexType>
<xs:sequence>
<xs:element ref="Headers"/>
<xs:element ref="Details"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Headers">
<xs:complexType>
<xs:sequence>
<xs:element name="e1"
type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:key name="key1">
<xs:selector
xpath=".//Renewals/Renewal/Headers"/>
<xs:field xpath="e1"/>
</xs:key>
</xs:element>
<xs:element name="Details">
<xs:complexType>
<xs:sequence>
<xs:element name="e1"
type="xs:string"/>
<xs:element name="e2"
type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:keyref name="fKey" refer="key1">
<xs:selector
xpath=".//Renewals/Renewal/Details"/>
<xs:field xpath="e1"/>
</xs:keyref>
</xs:element>
</xs:schema>
=====================================
The code I am using is the following:
=====================================
public string ValidateXml(string inXMLLocation, string
inXSDLocation)
{
string errorsList = string.Empty;
try
{
// Read the xml file, and send this to
validator.
XmlTextReader xmlReader = new
XmlTextReader(inXMLLocation);
// load the schema file into an
XmlTextReader object
XmlTextReader xsdReader = new
XmlTextReader(inXSDLocation);
XmlSchemaCollection sc = new
XmlSchemaCollection();
// add the ValidationEventHandler to the
schema collection
sc.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
// add the schema to the
XmlSchemaCollection object
sc.Add("", xsdReader);
// create an XmlValidatingReader object
based on the Xml document
XmlValidatingReader rdr = new
XmlValidatingReader(xmlReader);
// set the ValidationType to a schema
rdr.ValidationType =
ValidationType.Schema;
rdr.Schemas.Add(sc);
rdr.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
while (rdr.Read());
errorsList = validationErrors;
//closing the readers and releasing the
handlers to the IFS docs.
//These readers were locking the files
when saving them to the IFS.
xmlReader.Close();
xsdReader.Close();
}
catch(Exception exc)
{
throw new Exception(string.Format("An
exception has occurred when executing method {0}.", (new
StackFrame()).GetMethod().Name), exc);
}
finally
{}
return errorsList;
}