如何创建的XElement与特定的命名空间? [英] How create XElement with specific namespace?
本文介绍了如何创建的XElement与特定的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题,在LinqToXml创造新的元素。 这是我的code:
I have problem with creating new element in LinqToXml. This is my code:
XNamespace xNam = "name";
XNamespace _schemaInstanceNamespace = @"http://www.w3.org/2001/XMLSchema-instance";
XElement orderElement = new XElement(xNam + "Example",
new XAttribute(XNamespace.Xmlns + "xsi", _schemaInstanceNamespace));
我想获得这样的:
I want to get this:
<name:Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
但在XML中,我总是得到这样的:
But in XML I always get this:
<Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="name">
我在做什么错了?
What I'm doing wrong?
推荐答案
&LT;名称:实例的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance&GT ;
未命名空间良好的为preFIX 名称
未声明。如此构建,与一个XML API是不可能的。你可以做的就是构建以下命名空间格式良好的XML
<name:Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
is not namespace well-formed as the prefix name
is not declared. So constructing that with an XML API is not possible. What you can do is construct the following namespace well-formed XML
<name:Example xmlns:name="http://example.com/name" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
与code
with the code
//<name:Example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:name="http://example.com/name"></name:Example>
XNamespace name = "http://example.com/name";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement example = new XElement(name + "Example",
new XAttribute(XNamespace.Xmlns + "name", name),
new XAttribute(XNamespace.Xmlns + "xsi", xsi));
Console.WriteLine(example);
这篇关于如何创建的XElement与特定的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文