加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用前缀创建XmlElement属性?

发布时间:2020-12-15 18:21:09 所属栏目:百科 来源:网络整理
导读:我需要能够在xml元素中定义带有前缀的属性. 例如… nc:Person s:id="ID_Person_01"/nc:Person 为了做到这一点,我虽然以下会有效. XmlElement TempElement = XmlDocToRef.CreateElement("nc:Person","http://niem.gov/niem/niem-core/2.0");TempElement.SetAt
我需要能够在xml元素中定义带有前缀的属性.

例如…

<nc:Person s:id="ID_Person_01"></nc:Person>

为了做到这一点,我虽然以下会有效.

XmlElement TempElement = XmlDocToRef.CreateElement("nc:Person","http://niem.gov/niem/niem-core/2.0");
TempElement.SetAttribute("s:id","http://niem.gov/niem/structures/2.0","ID_Person_01");

不幸的是,当我收到下面的错误时,XmlElement.SetAttribute(string,string,string)似乎不支持解析前缀.

The ‘:’ character,hexadecimal value 0x3A,cannot be included in a name.

如何定义带前缀的属性?

解决方法

如果您已在根节点中声明了命名空间,则只需更改SetAttribute调用以使用未加前缀的属性名称.因此,如果您的根节点定义了这样的命名空间:
<People xmlns:s='http://niem.gov/niem/structures/2.0'>

您可以执行此操作,该属性将获取您已经建立的前缀:

// no prefix on the first argument - it will be rendered as
// s:id='ID_Person_01'
TempElement.SetAttribute("id","ID_Person_01");

如果您还没有声明命名空间(及其前缀),那么三字符串XmlDocument.CreateAttribute重载将为您完成:

// Adds the declaration to your root node
var attribute = xmlDocToRef.CreateAttribute("s","id","http://niem.gov/niem/structures/2.0");
attribute.InnerText = "ID_Person_01"
TempElement.SetAttributeNode(attribute);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读