Missing schema targetNamespace=\"\".  
Author Message
wjousts





PostPosted: XML and the .NET Framework, Missing schema targetNamespace=\"\". Top

I am trying to serialize an object that implements the IXmlSerializable interface and has the XmlSchemaProvider attribute but I keep getting the an exception thrown with the message "Missing schema targetNamespace=\"\"." when I try and serialize an object that contains my object.

Here's the code I'm using to generate the schema:

public static XmlQualifiedName CreateSchema(XmlSchemaSet xs)

{

XmlSchema schema = new XmlSchema();

schema.Id = "PresentationSchema";

schema.TargetNamespace = " http://www.hide-link.com/ ;;

XmlSchemaComplexType root = new XmlSchemaComplexType();

root.Name = "Presentation";

XmlSchemaSequence rootSeq = new XmlSchemaSequence();

root.Particle = rootSeq;

XmlSchemaComplexType indexElement = new XmlSchemaComplexType();

indexElement.Name = "Indexes";

XmlSchemaElement indexRef = new XmlSchemaElement();

indexRef.RefName = new XmlQualifiedName("Indexes", " http://www.hide-link.com/ ;);

rootSeq.Items.Add(indexRef);

XmlSchemaSequence ids = new XmlSchemaSequence();

ids.MinOccurs = 0;

ids.MaxOccursString = "unbounded";

XmlSchemaElement idRef = new XmlSchemaElement();

idRef.SchemaTypeName = new XmlQualifiedName("integer"," http://www.hide-link.com/ ;);

idRef.Name = "ID";

ids.Items.Add(idRef);

indexElement.Particle = ids;

schema.Items.Add(root);

schema.Items.Add(indexElement);

return new XmlQualifiedName("Presentation");

}

And it produces this schema:

< xml version="1.0" encoding="utf-8" >
<xs:schema targetNamespace=" http://www.hide-link.com/ " id="PresentationSchema" xmlns:xs=" http://www.hide-link.com/ ">
<xs:complexType name="Presentation">
<xs:sequence>
<xs:element ref="xs:Indexes" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Indexes">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="ID" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Can somebody please tell me what is going wrong here These schema are very confusing.

Thanks



.NET Development25  
 
 
Priya Lakshminarayanan





PostPosted: XML and the .NET Framework, Missing schema targetNamespace=\"\". Top

In your CreateSchema() method, you should add the schema of your object to the XmlSchemaSet parameter that is passed in. You are missing the following line in your code:

xs.Add(null, schema);

As a side note, it is not recommended to specify targetNamespace of your custom schema as the W3C schema namespace(http://www.w3.org/2001/XMLSchema) as it means that you are extending the Schema for Schema. You should change your targetNamespace attribute to something like "MyPresentationSchema".

Thanks,

Priya


 
 
wjousts





PostPosted: XML and the .NET Framework, Missing schema targetNamespace=\"\". Top

Thanks for your reply. That seems to have fixed that particular problem. I've changed the code around a bit, but now I'm getting this error:

Schema type information provided by IXmlSerializable is invalid: Type 'IndexType' is not declared.

Here's the code now:

public static XmlQualifiedName CreateSchema(XmlSchemaSet xs)

{

XmlSchema schema = new XmlSchema();

schema.Id = "PresentationSchema";

schema.TargetNamespace = "MyPresentationSchema";

XmlSchemaComplexType root = new XmlSchemaComplexType();

root.Name = "Presentation";

XmlSchemaSequence rootSeq = new XmlSchemaSequence();

root.Particle = rootSeq;

XmlSchemaComplexType indexElement = new XmlSchemaComplexType();

indexElement.Name = "IndexType";

XmlSchemaElement indexRef = new XmlSchemaElement();

indexRef.SchemaTypeName = new XmlQualifiedName("IndexType");

indexRef.Name = "Indexes";

rootSeq.Items.Add(indexRef);

XmlSchemaSequence ids = new XmlSchemaSequence();

ids.MinOccurs = 0;

ids.MaxOccursString = "unbounded";

XmlSchemaElement idRef = new XmlSchemaElement();

idRef.SchemaTypeName = new XmlQualifiedName("integer","http://www.w3.org/2001/XMLSchema");

idRef.Name = "ID";

ids.Items.Add(idRef);

indexElement.Particle = ids;

schema.Items.Add(indexElement);

schema.Items.Add(root);

xs.Add(schema);

return new XmlQualifiedName("Presentation");

}

Which produces this schema:

< xml version="1.0" encoding="utf-8" >
<xs:schema xmlns:tns="MyPresentationSchema" targetNamespace="MyPresentationSchema" id="PresentationSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="IndexType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="ID" type="xs:integer" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Presentation">
<xs:sequence>
<xs:element name="Indexes" type="IndexType" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Which seems fine to me because it defines 'IndexType'.

Any ideas

Thanks


 
 
Priya Lakshminarayanan





PostPosted: XML and the .NET Framework, Missing schema targetNamespace=\"\". Top

If the targetNamespace is not the default namespace in your schema, you have to namespace qualify all references to type names, element names etc

indexRef.SchemaTypeName = new XmlQualifiedName("IndexType"); should be

indexRef.SchemaTypeName = new XmlQualifiedName("IndexType", "MyPresentationSchema");

and the above change should change this <xs:element name="Indexes" type="IndexType" /> to

<xs:element name="Indexes" type="tns:IndexType" />

Thanks,

Priya