locked
XmlSerializerNamespaces missing prefix RRS feed

  • Question

  • User1536465747 posted

    Hello,

    I have the following classes to be serialized:

        [XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class Envelope
        {        
            public Envelope() { }
            public Body Body { get; set; }
        }
    
        [XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class Body
        {
            public Body() { }
            public InfoDetails infoDetails { get; set; }
        }

    When I serialize those classes like following:

    var myNamespaces = new XmlSerializerNamespaces();
    myNamespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
    //-- another code here
    mySerializer.Serialize(myWriter, envelope, myNamespaces);

    I end up with an XML like this:

    <?xml version="1.0" encoding="utf-8"?>
    <Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Body>
        <soapenv:infoDetails>
        ...
       </soapenv:infoDetails>
      </soapenv:Body>
    </Envelope>

    As you can see the root element is without a prefix soapenv. 

    What I am doing wrong if I want the Envelope element to also has a prefix soapenv?

    Thank you

    Thursday, February 27, 2020 1:23 AM

Answers

  • User-719153870 posted

    Hi KulerMaster,

    Instead of XmlType, you need to use XmlRoot for your Envelope class.

    I built a demo based on the code from Serialize(XmlWriter, Object, XmlSerializerNamespaces).

    public class Program
        {
            static void Main(string[] args)
            {
                Program t = new Program();
                // Write a purchase order.
                t.SerializeObject(@"C:\Users\yangsh\Desktop\Files\simple.xml");
            }
    
            [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public class OrderedItem
            {
                [XmlElement(Namespace = "http://www.cpandl.com")]
                public string ItemName;
                [XmlElement(Namespace = "http://www.cpandl.com")]
                public string Description;
                [XmlElement(Namespace = "http://www.cohowinery.com")]
                public decimal UnitPrice;
                [XmlElement(Namespace = "http://www.cpandl.com")]
                public int Quantity;
                [XmlElement(Namespace = "http://www.cohowinery.com")]
                public decimal LineTotal;
                // A custom method used to calculate price per item.
                public void Calculate()
                {
                    LineTotal = UnitPrice * Quantity;
                }
            }
    
            private void SerializeObject(string filename)
            {
                Console.WriteLine("Writing With TextWriter");
                // Create an XmlSerializer instance using the type.
                XmlSerializer serializer =
                new XmlSerializer(typeof(OrderedItem));
                OrderedItem i = new OrderedItem();
                i.ItemName = "Widget";
                i.Description = "Regular Widget";
                i.Quantity = 10;
                i.UnitPrice = (decimal)2.30;
                i.Calculate();
    
                // Create an XmlSerializerNamespaces object.
                XmlSerializerNamespaces ns =
                new XmlSerializerNamespaces();
                // Add two namespaces with prefixes.
                ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                ns.Add("inventory", "http://www.cpandl.com");
                ns.Add("money", "http://www.cohowinery.com");
                // Create a StreamWriter to write with.
                TextWriter writer = new StreamWriter(filename);
                /* Serialize using the object using the TextWriter 
                and namespaces. */
                serializer.Serialize(writer, i, ns);
                writer.Close();
            }
        }

    The result - simple.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <soapenv:OrderedItem xmlns:money="http://www.cohowinery.com" xmlns:inventory="http://www.cpandl.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <inventory:ItemName>Widget</inventory:ItemName>
      <inventory:Description>Regular Widget</inventory:Description>
      <money:UnitPrice>2.3</money:UnitPrice>
      <inventory:Quantity>10</inventory:Quantity>
      <money:LineTotal>23.0</money:LineTotal>
    </soapenv:OrderedItem>

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2020 4:35 AM