1

I have an object and I would like to serialize it. I would like to add the namespaces to a specific element of the xml document. I have created several .xsd files from 1 default xml. I use XmlSerializer.

The namespace should be described in the <sos:element. That is what I want:

<env:root
  xmls:env ="httpenv"
  xmlns:sos="httpsos">
   <env:body>
     <sos:element 
       xmlns:abc="" <--------------my namespaces are located in <sos:element
       ...

if I use something like

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("abc", "httpabc");
ns.add....
StringWriter stringWriter = new StringWriter();   
serializer.Serialize(stringWriter, ObjectToSerialize, ns);

I will get the following

<env:root
  xmls:env ="httpenv"
  xmlns:sos="httpsos"
  xmlns:abc="" <-------------I do not want it here; I want it in <sos:element
   <env:body>
     <sos:element> 
      ...

Is there a way to specify where (in which element) I would like to have my namespaces declared or are they all declared in the root element?

4
  • How are the XSD files relevant for the answer to your question? Commented Feb 15, 2014 at 13:52
  • The mention of xsd files, was just a small sideinfo. Btw, why did I get a downvote.. Commented Jul 10, 2015 at 13:45
  • 4 years later and i got a similar question, and your question seems legitimate, don't understand the downvote either. Commented Jun 28, 2018 at 2:08
  • Maybe some trolls :) Well, let's upvote the question!! Commented Jun 29, 2018 at 9:15

1 Answer 1

6

From XML perspective, it doesn't matter where the XML namespace is defined. If you need the XML namespace declaration at a particular place, there's probably something wrong with the component that parses the XML.

Well, anyway, this is what I came up with:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace XMLNamespaceChangeSerialization
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var serialize = Serialize();
            Console.WriteLine(serialize);
            Console.ReadLine();
        }

        private static string Serialize()
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add("env", "httpenv");
            // Don't add it here, otherwise it will be defined at the root element
            // ns.Add("sos", "httpsos");
            var stringWriter = new StringWriter();
            var serializer = new XmlSerializer(typeof (RootClass), "httpenv");
            serializer.Serialize(stringWriter, new RootClass(), ns);
            return stringWriter.ToString();
        }
    }


    [Serializable]
    [XmlRoot(ElementName = "root")]
    public class RootClass
    {
        [XmlElement(ElementName = "body", Namespace = "httpenv")]
        public BodyClass body = new BodyClass();
    }

    [Serializable]
    public class BodyClass
    {
        [XmlElement( ElementName = "element", Namespace = "httpsos")]
        public SOSClass element = new SOSClass();
    }

    [Serializable]
    public class SOSClass
    {
        // This will be used by XML serializer to determine the namespaces
        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(
                    new[] { new XmlQualifiedName("sos", "httpsos"), });
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Just add: ns.Add("sos", "httpsos"); to your Serialize(). And in your SOSClass change the public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("sos", "httpsos"), }); to public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("abc", "WriteNamespaceHere"), }); and you are done
@eyalsn: thanks for the hint. Indeed, I explained how to move the sos namespace to somewhere else. With the concept behind it, the reader should now be able to move any namespace anywhere.
@ThomasWeller can i do what you have shown without editing the original Class ? Because my Class is generated by svcUtil using a wsdl definition and i don't what to touch it, instead i want to manipulate the Serializer object such that when it serialises, it adds a prefix / namespace to a specific element. Is this possible ?
@joedotnot: not sure if it can be done via the serializer. You could apply a transformation on the serialized XML which does that. Maybe you want to ask your own question here on SO.

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.