none
XML namespace prefix on class definition RRS feed

  • Question

  • I have an XML definition I use to deserialize incoming text based XML and similarly serialize instances for text output. On the class definition I set [XmlRoot("MyType", Namespace = "urn:foo.domain.com")] and the prefixed input is deserialized just fine as that namespace is set as the default. When serializing however, the text output structure is fine however the receiving end is hard coded to look for the prefixed elements, regardless of them utilizing the default namespace.

    I know I can add the desired prefix while serializing which is what I am doing, is there an attribute I can decorate the class with to instruct XmlSerializer to add the desired prefix?

    Thanks!

    Tuesday, April 14, 2015 4:15 PM

Answers

  • >>I know I can add the desired prefix while serializing which is what I am doing, is there an attribute I can decorate the class with to instruct XmlSerializer to add the desired prefix?

    No, it is the reponsibility of the serializer to give the namespace a specific prefix. By default, it is not necassary to use any prefix at all:

    <Node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="foo.domain.com">
      <childNode>this is the value</childNode>
    </Node>

    >>It seems that if you want a prefix you are stuck adding it during serialization?

    Correct. There is no property of the XmlRootAttribute or another attribute that tells the serializer to use a specific prefix.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by Ritmo2k Wednesday, April 15, 2015 4:16 PM
    Wednesday, April 15, 2015 3:47 PM

All replies

  • When serializing however, the text output structure is fine however the receiving end is hard coded to look for the prefixed elements, regardless of them utilizing the default namespace.

    I know I can add the desired prefix while serializing which is what I am doing, is there an attribute I can decorate the class with to instruct XmlSerializer to add the desired prefix?

    Hi,

    I'm not completely clear about your question actually. Maybe you could give us a simple sample XML and the code to demonstrate your scenario and tell us what you're expecting.

    System.Xml.Serialization provides us XmlSerializerNamespaces class which you could use to process the namespaces of the xml. If you mean you want to get XML elements with namespace prefix like this:

    <urn:MyType></urn:MyType>

    You could try sample code here:

    [XmlRoot("Node", Namespace = "foo.domain.com")]
        public class MyType
        {
            [XmlElement("childNode")]
            public string Value { get; set; }
        }
    
        static class Program
        {
            static void Main()
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("urn", "foo.domain.com");
                XmlSerializer xser = new XmlSerializer(typeof(MyType));
                xser.Serialize(Console.Out, new MyType() { Value="this is the value"}, ns);
    
                Console.Read();
            }
        }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 15, 2015 6:47 AM
    Moderator
  • Hi Caillen,
    Your example is exactly how I do it. However, I am wondering if one can avoid declaring a namespace during serialization and mark the class with the prefix and namespace. It seems that if you want a prefix you are stuck adding it during serialization?

    Thanks!
    Wednesday, April 15, 2015 3:27 PM
  • >>I know I can add the desired prefix while serializing which is what I am doing, is there an attribute I can decorate the class with to instruct XmlSerializer to add the desired prefix?

    No, it is the reponsibility of the serializer to give the namespace a specific prefix. By default, it is not necassary to use any prefix at all:

    <Node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="foo.domain.com">
      <childNode>this is the value</childNode>
    </Node>

    >>It seems that if you want a prefix you are stuck adding it during serialization?

    Correct. There is no property of the XmlRootAttribute or another attribute that tells the serializer to use a specific prefix.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by Ritmo2k Wednesday, April 15, 2015 4:16 PM
    Wednesday, April 15, 2015 3:47 PM