Question

I am using C# and have a question in relation to de serializing an XML string.

Here is my code to de serialize:

public object XmlDeserializeFromString(string objectData, Type type)
{
    var serializer = new XmlSerializer(type);
    object result;

    using (TextReader reader = new StringReader(objectData))
    {
        result = serializer.Deserialize(reader);
    }

    return result;
}

The following XML works with the above function:

<House>
<address>21 My House</address>
<id>1</id>
<owner>Optimation</owner>
</House>

However, the XML from my Web API application does not:

<House xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MVCwithWebAPIApplication.Models">
<address>21 My House</address>
<id>1</id>
<owner>Optimation</owner>
</House>

How can I get the XmlDeserializeFromString function to work with the XML from my Web API application?

Was it helpful?

Solution

The xml return from web api has a default namespace inside. To deserialize this xml into a memory object (defined by a c# class), XmlSerialize has to know whether the class belongs to that namespace or not. This is specified by the property 'Namespace' in RootAttribute attached to that class. If the namespace in xml matches to the declared namespace in c# class, then the xml is successfully deserialized. Otherwise deserialization fails.

More information about xml namespace please see http://www.w3schools.com/xml/xml_namespaces.asp

below is the demo solution for your reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication8 {
    class Program {
        static void Main(string[] args) {
            var s1 = "<House><address>21 My House</address><id>1</id><owner>Optimation</owner></House>";
            var s2 = "<House xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MVCwithWebAPIApplication.Models\"><address>21 My House</address><id>1</id><owner>Optimation</owner></House>";
            House house = (House)XmlDeserializeFromString(s2, typeof(House));
            Console.WriteLine(house.ToString());
            Console.Read();
        }

        public static Object XmlDeserializeFromString(string objectData, Type type) {
            var serializer = new XmlSerializer(type);
            object result;

            using (TextReader reader = new StringReader(objectData)) {
                result = serializer.Deserialize(reader);
            }

            return result;
        }


    }

    //this is the only change
    [XmlRoot(Namespace="http://schemas.datacontract.org/2004/07/MVCwithWebAPIApplication.Models")]
    public class House {
        public String address { get; set; }
        public String id { get; set; }
        public String owner { get; set; }

        public override string ToString() {
            return String.Format("address: {0} id: {1} owner: {2}", address, id, owner);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top