Deserializing XML with namespace and multiple nested elements

前端 未结 2 1121
不思量自难忘°
不思量自难忘° 2020-12-20 20:25

I am trying to deserialize the following xml



  

        
2条回答
  •  伪装坚强ぢ
    2020-12-20 20:47

    The problem is that the namespace of myrootNS class is incorrect because it doesn't match the expected namespace in the XML.

    [XmlRoot("myroot", Namespace = "http://jeson.com/")]
    public class myrootNS
    {
        [XmlElement(Namespace = "")]
        public item[] item { get; set; }
    }
    

    Notice that the Namespace property value has a trailing /. This is my deserialize method:

    static T Deserialize(string xml)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        XmlReaderSettings settings = new XmlReaderSettings();
        using (StringReader textReader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
            {
                return (T)serializer.Deserialize(xmlReader);
            }
        }
    }
    

提交回复
热议问题