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

标签 c# xml rdf xml-deserialization

我正在尝试将以下 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 属性却没有。我做错了什么?

最佳答案

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

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

关于c# - XML反序列化问题(带有命名空间的属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2400720/

相关文章:

c# - 如何将 .NET C# 项目添加到网站?

c# - 使用 C# DotNetZip 在内存中提取 zip 文件失败

xml - ruby 中/来自 ruby 的 XSLT 处理

xml - cvc 模式有效 : Value 'A' is not facet-valid with respect to pattern '^[A-Za-z]?$' for type 'whatever'

javascript - Angular JS 应用程序无法通过 Wcf 服务从 Sql 数据库检索数据

c# - 为什么我必须明确询问可空 bool 变量是否为真?

java - 如何在 Spring 属性中进行算术运算?

php - 使用 PHP 解析 88 GB rdf

rdf - 在RDF中自动添加具有增量值的记录

rdf - 如何在现有数据集上使用替代/新本体