本帖最后由 twoconk 于 2015-02-10 14:41:42 编辑

解决方案 »

  1.   

    list能反序列化出来,但是entry没有解析出来    [XmlRootAttribute("list")]
        public class RLSList
        {
            [XmlAttribute(AttributeName = "name")]
            public string name
            {
                get;
                set;
            }        [XmlElementAttribute("rl:entry")]
            public List<RlsEntry> entry
            {
                get;
                set;
            }
        }    [XmlRootAttribute("rl:entry")]
        public class RlsEntry
        { 
            [XmlAttribute(AttributeName = "uri")]
            public string uri
            {
                get;
                set;
            }    }
      

  2.   

    这个可以用命令行的xsd来对xml生成xsd和对应的cs类(如果需要再自己改改),也可以用VS2012开始自带的"Paste XML As Classes"功能,参考这里,比手写方便准确。比如你这个xml使用xsd生成的代码如下://------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.34209
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------// 
    // This source code was auto-generated by xsd, Version=4.0.30319.33440.
    // 
    namespace Rls {
        using System.Xml.Serialization;
        
        
        /// <res/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:ietf:params:xml:ns:rls-services")]
        [System.Xml.Serialization.XmlRootAttribute("rls-services", Namespace="urn:ietf:params:xml:ns:rls-services", IsNullable=false)]
        public partial class rlsservices {
            
            private rlsservicesService[] itemsField;
            
            /// <res/>
            [System.Xml.Serialization.XmlElementAttribute("service")]
            public rlsservicesService[] Items {
                get {
                    return this.itemsField;
                }
                set {
                    this.itemsField = value;
                }
            }
        }
        
        /// <res/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:ietf:params:xml:ns:rls-services")]
        public partial class rlsservicesService {
            
            private rlsservicesServiceList[] listField;
            
            private rlsservicesServicePackages[] packagesField;
            
            private string uriField;
            
            /// <res/>
            [System.Xml.Serialization.XmlElementAttribute("list")]
            public rlsservicesServiceList[] list {
                get {
                    return this.listField;
                }
                set {
                    this.listField = value;
                }
            }
            
            /// <res/>
            [System.Xml.Serialization.XmlElementAttribute("packages")]
            public rlsservicesServicePackages[] packages {
                get {
                    return this.packagesField;
                }
                set {
                    this.packagesField = value;
                }
            }
            
            /// <res/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string uri {
                get {
                    return this.uriField;
                }
                set {
                    this.uriField = value;
                }
            }
        }
        
        /// <res/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:ietf:params:xml:ns:rls-services")]
        public partial class rlsservicesServiceList {
            
            private entry entryField;
            
            private string nameField;
            
            /// <res/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace="urn:ietf:params:xml:ns:resource-lists")]
            public entry entry {
                get {
                    return this.entryField;
                }
                set {
                    this.entryField = value;
                }
            }
            
            /// <res/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string name {
                get {
                    return this.nameField;
                }
                set {
                    this.nameField = value;
                }
            }
        }
        
        /// <res/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:ietf:params:xml:ns:resource-lists")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:ietf:params:xml:ns:resource-lists", IsNullable=false)]
        public partial class entry {
            
            private string uriField;
            
            /// <res/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string uri {
                get {
                    return this.uriField;
                }
                set {
                    this.uriField = value;
                }
            }
        }
        
        /// <res/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:ietf:params:xml:ns:rls-services")]
        public partial class rlsservicesServicePackages {
            
            private string packageField;
            
            /// <res/>
            public string package {
                get {
                    return this.packageField;
                }
                set {
                    this.packageField = value;
                }
            }
        }
    }
      

  3.   

    OK,感谢,同时给entry加上namespace就OK了
        [XmlRootAttribute("list")]
        public class RLSList
        {
            [XmlAttribute(AttributeName = "name")]
            public string name
            {
                get;
                set;
            }        [XmlElementAttribute("entry", Namespace = "urn:ietf:params:xml:ns:resource-lists")]
            public List<RlsEntry> entry
            {
                get;
                set;
            }
        }    [XmlRootAttribute("entry", Namespace = "urn:ietf:params:xml:ns:resource-lists")]
        public class RlsEntry
        { 
            [XmlAttribute(AttributeName = "uri")]
            public string uri
            {
                get;
                set;
            }    }