如何创建XmlElement的前缀属性? [英] How to create XmlElement attributes with prefix?

查看:447
本文介绍了如何创建XmlElement的前缀属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够定义与在一个xml元素的前缀的属性

I need to be able to define an attribute with a prefix in a xml element.

有关实例...

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

在为了做到这一点,我虽然,以下会工作。

In order to do this I though that the following would have worked.

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(字符串,字符串,字符串)似乎并不支持解析前缀我收到以下错误

Unfortunately, XmlElement.SetAttribute(string, string, string) does not seem to support parsing the prefix as I receive the error below.

在':'字符,十六进制值0x3A,不能包含在$名称

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

我将如何定义与前缀属性?

How would I define an attribute with prefix?

推荐答案

如果你已经宣布根节点的命名空间,你只需要修改的setAttribute 呼叫使用加前缀的属性名称。所以,如果你的根节点定义这样一个命名空间:

If you've already declared your namespace in the root node, you just need to change the SetAttribute call to use the unprefixed attribute name. So if your root node defines a namespace like this:

<People xmlns:s='http://niem.gov/niem/structures/2.0'>

您可以做到这一点,该属性将拿起你已经建立了前缀:

You can do this and the attribute will pick up the prefix you've already established:

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

如果您还没有声明的名称空间(和它的前缀),三串的 XmlDocument.CreateAttribute 超载​​会为你做它:

If you have not yet declared the namespace (and its prefix), the three-string XmlDocument.CreateAttribute overload will do it for you:

// 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);

这篇关于如何创建XmlElement的前缀属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆