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?

7
  • 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. Commented Jun 14, 2012 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. Commented Jun 14, 2012 at 21:29
  • XmlAttribute is only allow for fields, properties, indexes and I have: Commented Jun 14, 2012 at 21:36
  • [XmlRoot(ElementName = "AmazonEnvelope", Namespace = "w3.org/2001/XMLSchema-instance")] [XmlAttribute(AttributeName="noNamespaceSchemaLocation")] public class XMLAmazonEnvelope { private ObservableCollection<XMLMessage> _Messages = new ObservableCollection<XMLMessage>(); [XmlElement(ElementName="Header")] public XMLHeader Header{get;set;} .... Commented Jun 14, 2012 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.) Commented Jun 14, 2012 at 21:40

1 Answer 1

0

Question already solved here:

How to add xml namespaces (Amazon Envelope)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.