.net 带冒号的 xml 元素名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792270/
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
xml element name with colon
提问by nelsonwebs
I'm working against a 3rd party xml api. They have defined a required xml structure similar to the following.
我正在针对第 3 方 xml api。他们定义了一个类似于下面的必需的 xml 结构。
<ns1:E xmlns:ns1="schema">
<ns1:B>
<ns2:S>
<ns2:V>
<ns2:Bl />
</ns2:V>
</ns2:S>
</ns1:B>
</ns1:E>
There is a SQL table with the information that I need to put into this xml format. I have a LINQ to SQL adapter to get the data and I'm using a System.Xmlto create an XML document.
有一个 SQL 表,其中包含我需要放入此 xml 格式的信息。我有一个 LINQ to SQL 适配器来获取数据,我正在使用 aSystem.Xml创建 XML 文档。
XmlDocument.CreateElement("ns1:E"); etc
This works fine as long as I remove the colons from the element names. With the colons, only the right hand side of the colon is in the element name. I know colons are a no-no but I don't have any control over what the 3rd party api is dictating.
只要我从元素名称中删除冒号,这就可以正常工作。对于冒号,只有冒号的右侧在元素名称中。我知道冒号是禁忌,但我无法控制第 3 方 api 所指示的内容。
What are my options for getting around this? Are there any useful ways of forcing the colons into the element names? I don't have to use XMLDocumentbut not sure what other method will get me there.
我有什么选择来解决这个问题?是否有任何有用的方法可以将冒号强制为元素名称?我不必使用XMLDocument但不确定还有什么其他方法可以让我到达那里。
UPDATE: I realize that the <ns1:refers to a namespace. And yes, there are 2. When writing out the XML, I can make it work if I say -
更新:我意识到 指的<ns1:是一个命名空间。是的,有 2 个。在写出 XML 时,我可以让它工作,如果我说 -
XmlDocument.CreateElement(ns1:E", "http://schema);
However, the XML output of this is
但是,它的 XML 输出是
<ns1:E xmlns:ns1="http://schema">
If I just say XmlDocument.CreateElement("ns1:E");with no uri, then the output is just <E>. I don't want the output to have the schema reference but I do need to have the prefix. The result I want to achieve is simply <ns1:E>. Both namspaces are declared at the top which I would think would mean I would have to declare them at every node.
如果我只是说XmlDocument.CreateElement("ns1:E");没有 uri,那么输出就是<E>. 我不希望输出具有架构引用,但我确实需要具有前缀。我想要达到的结果很简单<ns1:E>。两个 namspaces 都在顶部声明,我认为这意味着我必须在每个节点上声明它们。
采纳答案by nelsonwebs
Okay, here it is.
好的,它来了。
XmlDocument.CreateElement("prefix", "name", "uri");
reference here if it helps someone else: http://msdn.microsoft.com/en-us/library/c22k3d47.aspx1
如果对其他人有帮助,请参考此处:http: //msdn.microsoft.com/en-us/library/c22k3d47.aspx 1
回答by Robert Rossney
The XML document you posted is not actually well-formed, because the ns2abbreviation assigned to many of the elements is not associated with a namespace. Fixed, it might look like this:
您发布的 XML 文档实际上格式不正确,因为ns2分配给许多元素的缩写与命名空间无关。固定,它可能看起来像这样:
<ns1:E xmlns:ns1="schema" xmlns:ns2="my-namespace">
<ns1:B>
<ns2:S>
<ns2:V>
<ns2:Bl />
</ns2:V>
</ns2:S>
</ns1:B>
</ns1:E>
The above XML document is semantically equivalent to this one:
上面的 XML 文档在语义上等同于这个:
<s:E xmlns:s="schema">
<s:B>
<S xmlns="my-namespace">
<m:V xmlns:m="my-namespace">
<s:Bl xmlns:s="my-namespace"/>
</m:V>
</S>
</s:B>
</s:E>
And to this one:
而这个:
<E xmlns="schema">
<B xmlns="schema">
<S xmlns="my-namespace">
<V>
<Bl/>
</V>
</S>
</B>
</E>
In all three cases, the Eand Belements are in the schemanamespace, while the S, V, and Blelements are in the my-namespacenamespace.
在所有三种情况下,E和B元素在schema命名空间,同时S,V和Bl元素都在my-namespace命名空间。
Namespace prefixes are helpful, but strictly speaking they're unnecessary. You can create XML documents, like the last example, that use no prefixes, and that declare the namespace explicitly for every element.
命名空间前缀是有帮助的,但严格来说它们是不必要的。您可以创建 XML 文档,就像上一个示例一样,不使用前缀,并且为每个元素显式声明命名空间。
If, in processing XML, you think you care what prefix a given element is using, you're almost certainly wrong. The only thing you care about is what namespace it belongs to. For instance, if I load anyof those three documents into an XmlDocument, the following code will write out the 'Bl' element:
如果在处理 XML 时,您认为您关心给定元素使用的是什么前缀,那么您几乎肯定是错误的。您唯一关心的是它属于哪个命名空间。例如,如果我将这三个文档中的任何一个加载到 an 中XmlDocument,以下代码将写出 'Bl' 元素:
XmlNamespaceManager ns = new XmlNamespaceManager(d.NameTable);
ns.AddNamespace("a", "schema");
ns.AddNamespace("b", "my-namespace");
Console.Write(d.SelectSingleNode("/a:E/a:B/b:S/b:V/b:Bl", ns).OuterXml);
When you say:
当你说:
I don't want the output to have the schema reference but I do need to have the prefix. The result I want to achieve is simply
<ns1:E>.
我不希望输出具有架构引用,但我确实需要具有前缀。我想要达到的结果很简单
<ns1:E>。
you are almost certainly in error. An element whose tag is ns1:Eis meaningless unless the ns1prefix is mapped to a namespace, either in that element or in one of its ancestors. (Also, a namespace is not a schema reference.) If you use
你几乎肯定是错误的。ns1:E除非ns1前缀映射到命名空间,否则其标记是无意义的元素,无论是在该元素中还是在其祖先之一中。(此外,命名空间不是架构引用。)如果您使用
CreateElement("ns1", "E", "schema");
to create the element, and then append it to an element that has already declared ns1as being the prefix for the schemanamespace, then the DOM will append the element without the namespace declaration, because in that context it isn't needed. If ns1isn't declared (or is declared as abbreviating some namespace other than schema), then the DOM will stick a namespace declaration onto the element as well as the prefix.
创建元素,然后将其附加到已声明ns1为schema命名空间前缀的元素,然后 DOM 将附加没有命名空间声明的元素,因为在该上下文中不需要它。如果ns1未声明(或声明为除 之外的某个命名空间的缩写schema),则 DOM 会将命名空间声明粘贴到元素以及前缀上。
tl;dr: You care about namespaces, not namespace prefixes.
tl; dr:您关心名称空间,而不是名称空间前缀。
回答by John Paulett
The colons indicate that those elements are in the ns1 namespace. This is needed when you are using multiple schemas. Assuming the document only uses ns1, it is essentially equivalent to not having any "ns1:" in those tags. See this guidefor more info.
冒号表示这些元素在 ns1命名空间中。当您使用多个模式时,这是必需的。假设文档只使用 ns1,它本质上等同于在这些标签中没有任何“ns1:”。有关更多信息,请参阅本指南。
回答by Timothy Byrd
(I know it's an old question, but I'm doing something equivalent and figured out how to wave the dead chicken to make it work. I'm documenting it so I can find it later.)
(我知道这是一个老问题,但我正在做一些等效的事情,并想出了如何挥动死鸡以使其工作。我正在记录它,以便以后可以找到它。)
string myName = "foo";
string ns_local = root.GetNamespaceOfPrefix("local");
string ns_x = root.GetNamespaceOfPrefix("x");
System.Xml.XmlNode newNode = doc.CreateNode(System.Xml.XmlNodeType.Element, "local", "MyTag", ns_local);
System.Xml.XmlNode attr = doc.CreateNode(System.Xml.XmlNodeType.Attribute, "Key", ns_x);
attr.Value = myName;
newNode.Attributes.SetNamedItem(attr);
attr = doc.CreateNode(System.Xml.XmlNodeType.Attribute, "MyAttr", "");
attr.Value = myName;
newNode.Attributes.SetNamedItem(attr);
nodeToAppendTo.AppendChild(newNode);
This gives me a tag that looks like
这给了我一个看起来像的标签
<local:MyTag x:Key="foo" MyAttr="foo" />
回答by sowmya palani
<ns1:E xmlns:ns1="schema">
<ns1:B>
<ns2:S>
<ns2:V>
<ns2:Bl />
</ns2:V>
</ns2:S>
</ns1:B>
</ns1:E>
So to get the output as above using Create XmlElement it could be achieved as below :
因此,要使用 Create XmlElement 获得上述输出,可以按如下方式实现:
CreateElement("ns1","E", "schema");
CreateElement("ns1","B", "schema");
CreateElement("ns2","S", "schema");
CreateElement("ns2","V", "schema");
CreateElement("ns2","B1", "schema");
Here Element "E" is the root element so it prefixes it with "ns1" along with the schema.
这里元素“E”是根元素,因此它与模式一起用“ns1”作为前缀。
Since all the other mentioned Elements like "B","S","V","B1" are child Elements of "E" it prefixes with the mentioned prefix but do notagain displays the schema for the child Elements.
由于所有其他提到的元素(如“B”、“S”、“V”、“B1”)都是“E”的子元素,因此它以提到的前缀为前缀,但不再显示子元素的架构。
And hence you get the desired output as above with schema in the root element and only prefix for child elements.
因此,您可以在根元素中使用架构获得上述所需的输出,并且只有子元素的前缀。
回答by Kaleb Brasee
The word before the colon is the namespace. If you want to have an element "ns1:E", you needs to create an element named "E" and set its namespace to "ns1".
冒号前的词是命名空间。如果你想要一个元素“ns1:E”,你需要创建一个名为“E”的元素并将其命名空间设置为“ns1”。

