How to add multiple namespace in root attribute in class object for XML?

Viewed 34

I have XML file with multiple namespace in root attribute but I want to serialize it with Class object so I have no idea how to add multiple namespace in class object. Please refer below example.

    <?xml version="1.0" encoding="UTF-8"?>
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                          xmlns="http://test.com/v8_0" 
                          xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/" 
                          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <SOAP-ENV:Header/>
        <SOAP-ENV:Body>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

C # class

[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    
    public class Envelop
    {
        public string Header { get; set; }

        [XmlAttribute(Namespace = "http://b2b.att.com/schema/OrderingMobility/v8_0")]
        public Body Body { get; set; }  
    }

    public class Body
    {

    }

Please help me to out of this task.

1 Answers

Try following. Each class (or element) can have a different URI :

            Envelop envelope = null;
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
            namespaces.Add(string.Empty, "http://test.com/v8_0");
            namespaces.Add("SOAPENC", "http://schemas.xmlsoap.org/soap/encoding/");
            namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
            namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create("filename", settings,);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelop));
            serializer.Serialize(writer, envelope, namespaces); 
Related