Hello Xml Gurus,
I'm trying to build an XML schema in memory using the System.Xml.XmlSchema
namespace objects, validate it, and then write it to a file. The problem
I'm facing is that XmlSchema.Write() is changing
<xsd:element minOccurs="1" name="AccountId" type="s-xsd:primarykey" />
to be
<xsd:element minOccurs="1" name="AccountId" xmlns:q1="s-xsd"
type="q1:primarykey" />
and I am also getting the following error messages by XmlSchemaSet.Compile()
Namespace 's-xsd' is not available to be referenced in this schema.
Type 's-xsd:primarykey' is not declared.
--------------------------------------------------------------------------------
My code basically looks like this
--------------------------------------------------------------------------------
XmlSchema schema = new XmlSchema();
schema.AttributeFormDefault = XmlSchemaForm.Unqualified;
schema.ElementFormDefault = XmlSchemaForm.Qualified;
schema.TargetNamespace = "http://www.baseUri.com/sxml/1.0";
schema.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
schema.Namespaces.Add("s-xml", "http://www.baseUri.com/sxml/1.0");
schema.Namespaces.Add("s-xsd", "http://www.baseUri.com/sxsd/1.0");
// <xsd:import...
XmlSchemaImport import = new XmlSchemaImport();
schema.Includes.Add(import);
import.Namespace = "http://www.baseUri.com/sxsd/1.0";
import.SchemaLocation = "s-xsd.xsd";
// <xsd:complexType...
XmlSchemaComplex complexType = new XmlSchemaComplexType();
schema.Items.Add(complexType);
complexType.Name = "Account";
// <xsd:sequence>
XmlSchemaSequence sequence = new XmlSchemaSequence();
complexType.Particle = sequence;
// <xsd:element...
XmlSchemaElement element = new XmlSchemaElement();
element.Name = "AccountId";
element.MinOccurs = 1;
element.MaxOccurs = 1;
element.SchemaType = new XmlQualifiedName("primarykey", "s-xsd");
// Compile schema and validate
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new
ValidationEventHandler(schemaSet_ValidationEventHa ndler);
schemaSet.Add(schema);
schemaSet.Compile();
// Write compiled schemas to output
foreach (XmlSchema compiledSchema in schemaSet.Schemas())
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("s-xml", "http://www.baseUri.com/sxml/1.0");
nsmgr.AddNamespace("s-xsd", "http://www.baseUri.com/sxsd/1.0");
compiledSchema.Write(Console.Out);
}
--------------------------------------------------------------------------------
And the output looks like this:
(Note that there are 2 schemas. The first is the one I
created through code, and the second is the one
referenced by the first)
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="IBM437"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.baseUri.com/sxml/1.0"
xmlns:s-xml="http://www.baseUri.com/sxml/1.0"
xmlns:s-xsd="http://www.baseUri.com/sxsd/1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import schemaLocation="s-xsd.xsd"
namespace="http://www.baseUri.com/sxsd/1.0" />
<xsd:complexType name="queue">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="queueid"
xmlns:q1="s-xsd" type="q1:primarykey" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<?xml version="1.0" encoding="IBM437"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.baseUri.com/sxsd/1.0"
xmlns:s-xsd="http://www.baseUri.com/sxsd/1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="primarykey">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
Can someone please tell me what I'm doing wrong? Is the
System.Xml.XmlSchema more pain than it's worth?
Thanks,
Ryan Parlee