简体   繁体   English

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

[英]XML deserialization problem (attribute with a namespace)

I'm trying to deserialize the following XML node (RDF actually) into a class. 我正在尝试将以下XML节点(实际上是RDF)反序列化为类。

<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; }
    }

The name element is parsed correctly but the about attribute isn't. name元素已正确解析但about属性未正确解析。 What am I doing wrong? 我究竟做错了什么?

You need to specify that the attribute will be namespace qualified. 您需要指定该属性将是名称空间限定的。

[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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM