0

I am using .NET 3.5 to serialise a class to Xml and to create an XSD schema. The generated XML references the XSD using the schema location attribute.

My solution is based on these answers: XmlSerialization and xsi:SchemaLocation (xsd.exe) and XML Serialization and Schema without xsd.exe

I add an attribute to my class to reference the XSD:

[XmlAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string XsiSchemaLocation = "MyNameSpace " + "MyNameSpace.xsd";

The problem is that the field XsiSchemaLocation ends up in my XSD file:

<xs:attribute xmlns:q1="http://www.w3.org/2001/XMLSchema-instance" ref="q1:schemaLocation" />

When I try to edit my serialised XML file auto-complete in Visual Studio doesn't work because of the above attribute and gives the below error:

The 'http://www.w3.org/2001/XMLSchema-instance:schemaLocation' attribute is not declared.

My current solution to remove the schema location attribute from the XSD is the below hack:

    XmlReflectionImporter importer = new XmlReflectionImporter();
    XmlSchemas schemas = new XmlSchemas();
    XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
    XmlTypeMapping map = importer.ImportTypeMapping(m_SerializedType);
    exporter.ExportTypeMapping(map);
    using (var tw = new StreamWriter(m_XsdPath))
    {
        //Hack to remove the schema location from the XSD.
        ((System.Xml.Schema.XmlSchemaComplexType)(schemas[0].Items[1])).Attributes.Clear();
        schemas[0].Write(tw);
    }

Is there a better way than forcibly removing the attribute. Something like an [XmlSchemaIgnore] attribute would be perfect.

Community
  • 1
  • 1
row1
  • 5,568
  • 3
  • 46
  • 72

1 Answers1

0

XML Serialization is meant to serialize your data. If the schemaLocation is part of your data, then you want it in your schema. If it's not in your data, then you shouldn't be serializing it.

Remember that schemaLocation is only a hint for tools that want to refer to the schema. It's not necessary in many cases (Visual Studio, for example).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • When I serialise my data schemaLocation correctly appears in the XML file as xsi:schemaLocation="MyNameSpace MyNameSpace.xsd". The problem is that schemaLocation should not be considered part of my XSD schema, it should be part of the imported schema (via XmlSchema.InstanceNamespace). I want intellisense to work in Visual Studio if someone is editing my serialised data, without schemaLocation intellisense does not work, so it is necessary in my case. – row1 Jul 29 '11 at 03:38
  • You're mistaken. Intellisense will work if the schema file is in the same project, or if you "manually" set the schema to use. Again, you can't have it both ways. – John Saunders Jul 29 '11 at 14:09
  • These files don't exist in a project so Intellisense doesn't work without schema location. I want configuration engineers to open this file on a remote server in Visual Studio and edit it, it is troublesome if they have to manually select the schema. The answers I linked to say that my usage of schema location is correct for serialisation, so it is unfortunate that it doesn't seem to work for XSD generation. – row1 Jul 30 '11 at 03:23
  • Those answers were hacks that only "work" if you don't also need the schema. If you're down for creating your own subclass of `XmlWriter`, then you could override the generation of the root element to include the `schemaLocation` attribute. – John Saunders Jul 30 '11 at 18:46