Unable to deserialize from serialized XML RRS feed

  • Question

  • User-1934530859 posted

    I am unable to deserialize from serialized XML that is generated from a RESTful WCF service. Below is the code. I do know that if I completely remove the xmlns attributes the XML is then well forms and can be deserialized.

    Class XML derives from:

    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Xml.Serialization;

    namespace MyNamespace
    [DataContract(Name = "MyClass", Namespace = "")]
    public class MyClass
    public MyClass()

    public MyClass(string _Total2, string _Total1)
    Total2 = _Total2;
    Total1 = _Total1;

    [DataMember(Name = "Total2", EmitDefaultValue = false)]
    public string Total2 { get; set; }

    [DataMember(Name = "Total1", EmitDefaultValue = false)]
    public string Total1 { get; set; }

    XML output:

    <GetResponse xmlns=\"http://tempuri.org/\"><GetResult xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Total1 xmlns=\"\">100</Total1><Total2 xmlns=\"\">200</Total2 ></GetResult></GetResponse>

    Deserialization method:

    public static T DeserializeFromXml<T>(string xml)
    T result;
    var ser = new XmlSerializer(typeof(T));
    using (TextReader tr = new StringReader(xml))
    result = (T)ser.Deserialize(tr);
    return result;

    Monday, June 3, 2013 1:40 PM

All replies

  • User-525215917 posted

    Take a look at this thread at stackoverflow. You should add one namespace to nametable and deserializer should ba happy with it. I got this problem solved by using arbitraty namespace. It just needs that there is something. Not sure why it is implemented this way.

    Monday, June 3, 2013 1:48 PM
  • User-1934530859 posted

    I removed the xmlns attributes and it deserialized but i consider this a hack solution. Can someone tell me WHY having the xmlns attributes causes it to crap out.

    Monday, June 3, 2013 4:57 PM
  • User260886948 posted


    If your xml was using a namespace it would have an xmlns at the root.

    For more information about the reason, please try to refer to this thread:

    Hope it can help you.

    Best Regards,
    Amy Peng 

    Wednesday, June 5, 2013 7:10 AM
  • User-1934530859 posted

    I don't see how that is going to help. First I am not manually serializing this I am doing this through a RESTful service.

            [WebInvoke(Method = "GET",
              ResponseFormat = WebMessageFormat.Xml,
              BodyStyle = WebMessageBodyStyle.Bare,
              UriTemplate = "MyMethod{Id}")]
            MyClass MyMethod(string Id);

    This is how the xml is being generated, so when would I be able to do this:

    if (elem.Attribute(XNamespace.Xmlns + "xsi") == null) { elem.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance")); } if (elem.Attribute(XNamespace.Xmlns + "xsd") == null) { elem.Add(new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema")); }

    Wednesday, June 5, 2013 7:36 AM