简体   繁体   English

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

[英]System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name

So I am using C# to create an XML document and using the System.Xml.Linq package.所以我使用 C# 创建一个 XML 文档并使用 System.Xml.Linq 包。

I have a value I want to set in my xml that contains the character ':'我想在我的 xml 中设置一个包含字符 ':' 的值

prefix:myVar

My code looks like this:我的代码如下所示:

node.setAttribute("prefix:myVar", "myVarValue");

Where node is an XElement其中nodeXElement

I get the following exception when that line gets executed:当该行被执行时,我收到以下异常:

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

I am guessing it's because the string prefix:myVar contains the character : .我猜这是因为字符串prefix:myVar包含字符: Is there a way around this?有没有解决的办法?

The : is not allowed as part of the name because it's reserved to separate the namespace prefix from the local name. :不允许作为名称的一部分,因为它被保留用于将命名空间前缀与本地名称分开。 In this context, prefix is a namespace prefix and myVar is the local name.在此上下文中, prefix是命名空间前缀,而myVar是本地名称。

The prefix is only valid if it is declared in the scope of the element it appears in, and it needs to have an associated namespace - which is omitted in your example, so I'll use http://example.com/ to illustrate:前缀只有在它出现的元素的范围内声明时才有效,并且它需要有一个关联的命名空间 - 在你的例子中被省略,所以我将使用http://example.com/来说明:

XNamespace ex = "http://example.com/";

var element = new XElement("foo",
    new XAttribute(XNamespace.Xmlns + "prefix", ex),
    new XAttribute(ex + "myVar", "value"));

Note the name of the attribute is ex + "myVar" .请注意,属性的名称是ex + "myVar" This will create an element like this:这将创建一个像这样的元素:

<foo xmlns:prefix="http://example.com/" prefix:myVar="value" />

I've explicitly added the declaration using new XAttribute(XNamespace.Xmlns + "prefix", ex) above, but note this isn't required.我已经使用上面的new XAttribute(XNamespace.Xmlns + "prefix", ex)明确添加了声明,但请注意这不是必需的。 If you omit it then one will be generated for you:如果您省略它,则会为您生成一个:

XNamespace ex = "http://example.com/";

var element = new XElement("foo",
    new XAttribute(ex + "myVar", "value"));

Will result in this:会导致这样:

<foo p1:myVar="value" xmlns:p1="http://example.com/" />

The two outputs are semantically identical.这两个输出在语义上是相同的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Xml.XmlException:“‘:’字符,十六进制值 0x3A,不能包含在名称中。” - System.Xml.XmlException: „The ':' character, hexadecimal value 0x3A, cannot be included in a name.” 获取System.Xml.XmlException:名称不能以&#39;&#39;字符开头,十六进制值为0x20。 第42行,位置36 - Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36 “&#39;:&#39;字符,十六进制值0x3A,不能包含在名称中” - “The ':' character, hexadecimal value 0x3A, cannot be included in a name” XmlWriter &#39;:&#39; 字符,十六进制值 0x3A,不能包含在名称中 - XmlWriter The ':' character, hexadecimal value 0x3A, cannot be included in a name &#39;:&#39; 字符,十六进制值 0x3A,不能包含在名称中 - The ':' character, hexadecimal value 0x3A, cannot be included in a name RestSharp 请求错误? , XML , &#39;:&#39; 字符,十六进制值 0x3A,不能包含在名称中 - Error in RestSharp Request? , XML , The ':' character, hexadecimal value 0x3A, cannot be included in a name XML-System.Xml.XmlException-十六进制值0x06 - XML - System.Xml.XmlException - hexadecimal value 0x06 Xml到列表代码出错。 &#39;:&#39;字符,十六进制值0x3A,不能包含在名称中 - Error in Xml to List code. The ':' character, hexadecimal value 0x3A, cannot be included in a name XmlDocument.Loadxml System.Xml.XmlException:名称不能以“ 8”字符开头-imageglue中的xml - XmlDocument.Loadxml System.Xml.XmlException: Name cannot begin with the '8' character - xml from imageglue 名称中不能包含&#39;/&#39;字符十六进制值0x2F的XmlException - XmlException for the '/' character hex value 0x2F cannot be included in a name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM