日期:2014-05-17  浏览次数:20989 次

如何解析不规则xml
不规则的 xml:

<code>0</code>
<message>Success</message>
<roles>
<role><name>Role1</name><selected>false</selected></role>
<role><name>Role2</name><selected>true</selected></role>
</roles>

前面是两个独立的字段
后面是一个表,这样的 xml 如何解析?

谢谢

------解决方案--------------------
这个挺规则的啊,就是缺了一个root的element。下面的代码加入<xml></xml>作为root


 //test xml
            string input = @"<xml>" +
                            @"<code>0</code>" +
                           @" <message>Success</message>" +
                           @"<roles>" +
                           @"<role><name>Role1</name><selected>false</selected></role>" +
                           @"<role><name>Role2</name><selected>true</selected></role>" +
                           @"</roles>" +
                           @"</xml>";



            XmlSerializer serializer = new XmlSerializer(typeof(Response), "");
            using (TextReader reader = new StringReader(input))
            {
                Response result = serializer.Deserialize(reader) as Response;
            }



   [Serializable]
    [XmlRoot(ElementName= "xml")]