5

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?

  • 1
    `xsi:noNamespaceSchemaLocation="amzn-envelope.xsd"` isn't a namespace declaration. You'll have to add an attribute `noNamespaceShcemaLocation` in the `xsi` namespace to the `AmazonEnvelope` element, but I can't really see a straightforward way to do it in the documentation unless you have access to the source of `XMLAmazonEnvelope`. In that case you could add a field/property to that class annotated with `[XmlAttribute]` with the name `noNamespaceSchemaLocation` and the value you want. – millimoose Jun 14 '12 at 21:11
  • Or maybe it's possible with access to the source by using `XmlAttributeOverrides`, but I've no idea exactly how that API works. – millimoose Jun 14 '12 at 21:29
  • XmlAttribute is only allow for fields, properties, indexes and I have: – user1359657 Jun 14 '12 at 21:36
  • [XmlRoot(ElementName = "AmazonEnvelope", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] [XmlAttribute(AttributeName="noNamespaceSchemaLocation")] public class XMLAmazonEnvelope { private ObservableCollection _Messages = new ObservableCollection(); [XmlElement(ElementName="Header")] public XMLHeader Header{get;set;} .... – user1359657 Jun 14 '12 at 21:37
  • So attach the `[XmlAttribute(AttributeName="noNamespaceSchemaLocation")]` to a string field with the value `"amzn-envelope.xsd"`. (And use the correct namespace in the attribute.) – millimoose Jun 14 '12 at 21:40
  • `[XmlRoot(ElementName = "AmazonEnvelope", Namespace = "http://w3.org/2001/XMLSchema-instance")]` is probably also wrong. `AmazonEnvelope` isn't a XML Schema element. Maybe you should brush up on what XML namespaces are first. – millimoose Jun 14 '12 at 21:41
  • In this link here that issue is already solved: [how to add xml namespaces (Amazon envelope][1] [1]: http://stackoverflow.com/questions/928126/how-to-add-xml-namespces – Mare Infinitus Jun 14 '12 at 22:08

1 Answers1

0

Question already solved here:

How to add xml namespaces (Amazon Envelope)

Community
  • 1
  • 1
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113