I need to generate something like this:
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
...
</Header>
</AmazonEnvelope>
I was trying something like this but it's not fully correctly:
XmlSerializerNamespaces nms = new XmlSerializerNamespaces();
nms.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nms.Add("noNamespaceSchemaLocation", "amzn-envelope.xsd");
XmlSerializer serializer = new XmlSerializer(typeof(XMLAmazonEnvelope));
StreamWriter writer = new StreamWriter(path);
serializer.Serialize(writer, objectToSave,nms);
writer.Close();
And result is:
<?xml version="1.0" encoding="utf-8"?>
<xsi:AmazonEnvelope xmlns:noNamespaceSchemaLocation="amzn-envelope.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsi:Header>
...
</xsi:Header>
</xsi:AmazonEnvelope>
And this is not exactly what I want. Any ideas, what should be done differently?
xsi:noNamespaceSchemaLocation="amzn-envelope.xsd"isn't a namespace declaration. You'll have to add an attributenoNamespaceShcemaLocationin thexsinamespace to theAmazonEnvelopeelement, but I can't really see a straightforward way to do it in the documentation unless you have access to the source ofXMLAmazonEnvelope. In that case you could add a field/property to that class annotated with[XmlAttribute]with the namenoNamespaceSchemaLocationand the value you want.XmlAttributeOverrides, but I've no idea exactly how that API works.[XmlAttribute(AttributeName="noNamespaceSchemaLocation")]to a string field with the value"amzn-envelope.xsd". (And use the correct namespace in the attribute.)