deserialize XML with multiple namespaces in C#

consider following XML -->

<?xml version="1.0" encoding="UTF-8"?>
<abc:Root
    xmlns:abc = "http://www.abc.com"
    xmlns:xyz = "http://www.xyz.com
    xmlns:xlink = "http://www.somelink.com"
    xmlns:xsi = "http://www.somelink.com/schema">

I have a class for the purpose of serialization/deserialization

[XmlRoot(Namespace="http://www.abc.com")]
public partial class Root{

}

How do i deserialize the above XML, so that I don't loose any of the additional namespaces?
with what I have, I ran a test

1. I deserialized the above XML , to create an object.
2. then i serialized the object in step 1, and looked at the XML.
3. The XMl generated is missing other namespace attribute in the root element . It only has the xmlns:abc attribute.

How do I make sure, i don't loose other namespace attribute when I deserialize?
novice programmerAsked:
Who is Participating?
 
Najam UddinCommented:
Following is the code of class and its serialization, you can check and work back...
[XmlRoot(Namespace = "http://www.aaa.com")]
    public class Root
    {
        [XmlElement(Namespace = "http://www.bbb.com")]
        public int FirstChild { get; set; }
        [XmlElement(Namespace = "http://www.aaa.com")]
        public Child SecondChild { get; set; }
    }

    [XmlType(Namespace = "http://www.ccc.com")]
    public class Child
    {
        [XmlAttribute("test")]
        public string FirstChildProp { get; set; }
        public string SecondChildProp { get; set; }
    }

Open in new window


 static void Main(string[] args)
        {
            Root root = new Root
            {
                FirstChild = 100,
                SecondChild = new Child
                {
                    FirstChildProp = "AAA",
                    SecondChildProp = "BBB"
                }
            };

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("aaa", "http://www.aaa.com");
            ns.Add("bbb", "http://www.bbb.com");
            ns.Add("ccc", "http://www.ccc.com");
            //serialize
            using (StringWriter textWriter = new StringWriter())
            {
                new XmlSerializer(root.GetType()).Serialize(textWriter, root, ns);
            }
            
            //deserialize
            StreamReader reader = new StreamReader("output.xml");
            var ob = (Root)serializer.Deserialize(reader);

}

Open in new window

0
 
novice programmerAuthor Commented:
@Najam Uddin - thanks.

one thing i was wondering about about namespaces , from your code

we can pass ns, only when we call Serialize function, not deserialize.

In my situation, I only need to deserialize. is there a way adding the namespace to the XmlSerializer, when I only want to call Deserialize.

 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("aaa", "http://www.aaa.com");
            ns.Add("bbb", "http://www.bbb.com");
            ns.Add("ccc", "http://www.ccc.com");
            //serialize
            using (StringWriter textWriter = new StringWriter())
            {
                new XmlSerializer(root.GetType()).Serialize(textWriter, root, ns);
            }
           
            //deserialize
            StreamReader reader = new StreamReader("output.xml");
            var ob = (Root)serializer.Deserialize(reader);
0
 
Najam UddinCommented:
XmlSerializer serializer = new XmlSerializer(typeof(Root));
StreamReader reader = new StreamReader("output.xml");
var ob = (Root)serializer.Deserialize(reader);
reader.Close();

Open in new window


This should deserialize it.  It's just your classes and their properties should be as in my example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.