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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2255311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:47:59  来源:igfitidea点击:

How to create XmlElement attributes with prefix?

c#xml

提问by Eddie

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

我需要能够在 xml 元素中定义一个带有前缀的属性。

For instance...

例如...

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

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

不幸的是, XmlElement.SetAttribute(string, string, string) 似乎不支持解析前缀,因为我收到以下错误。

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

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

How would I define an attribute with prefix?

我将如何定义带有前缀的属性?

采纳答案by Jeff Sternal

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

如果您已经在根节点中声明了名称空间,则只需更改SetAttribute调用以使用不带前缀的属性名称。因此,如果您的根节点定义了这样的命名空间:

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

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

如果您尚未声明命名空间(及其前缀),则三字符串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);

回答by Jeff Hornby

Try creating the attribute directly and adding it to the element:

尝试直接创建属性并将其添加到元素中:

XmlAttribute attr = XmlDocToRef.CreateAttribute("s", "id", "http://niem.gov/niem/structures/2.0");
attr.InnerText = "ID_Person_01";
TempElement.Attributes.Append(attr);

回答by Peter Jacoby

The XMLDocument.CreateAttributemethod can take 3 strings: specified Prefix, LocalName, and NamespaceURI. You could then add the attribute to the element. Something like this might work for you:

XMLDocument.CreateAttribute方法可以采取3个字符串:指定的前缀,localName和的namespaceURI。然后,您可以将属性添加到元素。像这样的事情可能对你有用:

XmlAttribute newAttribute = XmlDocToRef.CreateAttribute("s", "id", "http://niem.gov/niem/structures/2.0");
TempElement.Attributes.Append(newAttribute):

回答by Yahoo Serious

Since my search kept taking me here, I'll answer this for XElement. I don't know if this solution is also valid for XmlElement, but it will hopefully at least help others using XElement, who end up here.

由于我的搜索一直将我带到这里,因此我将为XElement. 我不知道这个解决方案是否也适用于XmlElement,但它希望至少可以帮助其他人使用XElement,他们最终在这里。

Based on thisI added xml:space="preserve"to all data-nodes in some template, before looking up and adding their contents. It's weird code IMO (I would prefer three parameters as shown above, but it does the job:

基于xml:space="preserve",在查找和添加其内容之前,我添加到某个模板中的所有数据节点。这是 IMO 的奇怪代码(我更喜欢上面显示的三个参数,但它可以完成工作:

 foreach (XElement lElement in root.Descendants(myTag))
 {
      lElement.Add(new XAttribute(root.GetNamespaceOfPrefix("xml") + "space", "preserve"));
 }