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

发布于 2024-08-25 00:40:47 字数 797 浏览 3 评论 0原文

我正在尝试将以下 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; }
    }

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

I'm trying to deserialize the following XML node (RDF actually) into a class.

<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. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-09-01 00:40:47

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

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

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文