When I use the schema collection to apply many schemas to
one XML instance document, I get an error if I do not
qualify every element with the appropriate namespace.
Both the W3C site and this article
(http://www.xfront.com/ZeroOneOrManyNamespaces.html) imply
that I can submit an XML instance without having to
qualify each element.
How do I accomplish this while still using .Net & the
Validating Reader?
Thanks,
Raj (Example code follows)
ra****************@yahoo.com
Example:
XSD Product
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.product.org"
xmlns="http://www.product.org"
elementFormDefault="unqualified">
<xsd:complexType name="ProductType">
<xsd:sequence>
<xsd:element name="Type" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
XSD Person
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.person.org"
xmlns="http://www.person.org"
elementFormDefault="unqualified">
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="SSN" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
XSD Company
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace=”http://www.company.org”
xmlns:per="http://www.person.org"
xmlns:prod="http://www.product.org"
xmlns="http://www.company.org"
elementFormDefault="qualified">
<xsd:element name="Company">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Person"
type="per:PersonType"
maxOccurs="unbounded"/>
<xsd:element name="Product"
type="prod:ProductType"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I should be able to submit XML like the following (note:
only one element has namespace qualification -> to the
parent XSD file):
<?xml version="1.0"?>
<c:Company xmlns:c="http://www.company.org">
<Person>
<Name>John Doe</Name>
<SSN>123-45-6789</SSN>
</Person>
<Product>
<Type>Widget</Type>
</Product>
</c:Company>
My code:
namespace ConsoleApplication5
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ValidationEventHandler
eventHandler = new ValidationEventHandler
(Class1.ShowCompileErrors);
XmlSchemaCollection
myschemacoll = new XmlSchemaCollection();
XmlValidatingReader vr;
FileStream stream;
try
{
stream = new
FileStream(@"c:\Staging\UDG\Phase Two\Technical
Design\CompTest.xml", FileMode.Open);
//Load the
XmlValidatingReader.
vr = new
XmlValidatingReader(stream, XmlNodeType.Element, null);
//Add the schemas
to the XmlSchemaCollection object.
myschemacoll.Add
("http://www.company.org", @"c:\Staging\UDG\Phase
Two\Technical Design\company.xsd");
myschemacoll.Add
("http://www.person.org", @"c:\Staging\UDG\Phase
Two\Technical Design\person.xsd");
myschemacoll.Add
("http://www.product.org", @"c:\Staging\UDG\Phase
Two\Technical Design\product.xsd");
vr.Schemas.Add
(myschemacoll);
vr.ValidationType
= ValidationType.Schema;
vr.ValidationEventHandler += eventHandler;
while (vr.Read())
{//Console.WriteLine(" :: " + vr.Name);
}
Console.WriteLine
("Validation completed");
}
//This code
catches any XML exceptions.
catch (XmlException XmlExp)
{
Console.WriteLine
("XML ERROR: " + XmlExp.Message);
}
//This code
catches any XML schema exceptions.
catch (XmlSchemaException
XmlSchemaExp)
{
Console.WriteLine
("SCHEMA ERROR: " + XmlSchemaExp.Message);
}
//This code
catches any standard exceptions.
catch (Exception
GeneralExp)
{
Console.WriteLine
(GeneralExp.Message);
}
finally
{
//Clean up.
Console.Read();
vr = null;
myschemacoll =
null;
stream = null;
}
}
public static void
ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine
("Validation Error: {0}", args.Message);
}
}
}
}