问题描述:

I am trying to understand how I can validate an XML file with an XSD.

I don't quite understand the namespaces.

If I have the code:

 XmlDocument xDoc = new XmlDocument();

xDoc.Load(file.InputStream);

xDoc.Schemas.Add("http://www.w3.org/2001/XMLSchema-instance", @"C:MyXSD.xsd");

ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);

xDoc.Validate(eventHandler);

I get the Error:

"The target namespace of an attribute declaration, whether local or global, must not match http://www.w3.org/2001/XMLSchema-instance."

What should I be setting the namespace to when I add the schema to the xDoc?

I have an xml like this:

<?xml version="1.0"?>

<rootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

// My Nodes

</rootNode>

and I created an XSD from that XML.

<?xml version="1.0" encoding="Windows-1252"?>

<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">

<xsd:element name="rootNode">

// My Nodes

</xsd:element>

</xs:schema>

网友答案:

In your scenario you can just pass null as the namespace to Schemas.Add, as you are not actually using namespaces, i.e. your XML document does not use a namespace (which would be specified either with the xmlns attribute or the use of a namespace prefix).

For more information on namespaces please see http://www.w3schools.com/xml/xml_namespaces.asp.

Note that http://www.w3.org/2001/XMLSchema-instance is a namespace used to define schema attributes that relates only to the schema infrastructure and not your specific XML schema.

相关阅读:
Top