代码之家  ›  专栏  ›  技术社区  ›  Johnny

XML反序列化问题(具有命名空间的属性)

  •  3
  • Johnny  · 技术社区  · 16 年前

    <rdf:Description rdf:about="http://d.opencalais.com/genericHasher-1/dae360d4-25f1-34a7-9c70-d5f7e4cfe175">
        <rdf:type rdf:resource="http://s.opencalais.com/1/type/em/e/Country"/>
        <c:name>Egypt</c:name>
    </rdf:Description>
    
    
        [Serializable]
        [XmlRoot(Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#", ElementName = "Description")]
        public class BasicEntity
        {
            [XmlElement(Namespace = "http://s.opencalais.com/1/pred/", ElementName = "name")]
            public string Name { get; set; }
            [XmlAttribute("about", Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#")]
            public string Uri { get; set; }
        }
    

    name元素已正确解析,但about属性未正确解析。我做错什么了?

    1 回复  |  直到 16 年前
        1
  •  6
  •   Lachlan Roche    16 年前

    您需要指定属性将是命名空间限定的。

    [Serializable]
    [XmlRoot(Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#", ElementName = "Description")]
    public class BasicEntity
    {
        [XmlElement(Namespace = "http://s.opencalais.com/1/pred/", ElementName = "name")]
        public string Name { get; set; }
    
        [XmlAttribute("about", Form=XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#")]
        public string Uri { get; set; }
    }