英文:
How can I remove an xmlns from an element or avoid adding it when creating it
问题
我需要创建一个看起来像这样的XML。
<trajectorys xmlns="http://www.witsml.org/schemas/1series" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4.1.1">
<trajectory uidWell="b213fb5d-d203-41e3-8d03-64d71d5d19c2" uidWellbore="c8ede8d3-7838-4287-aad2-3da717150dff" uid="31EXX40">
</trajectory>
</trajectorys>
我的代码是。
var xDoc = new XDocument();
XNamespace ns1 = "http://www.witsml.org/schemas/1series";
XNamespace ns2 = "http://www.w3.org/2001/XMLSchema-instance";
var root = new XElement(ns1 + "trajectorys",
new XAttribute("xmlns", "http://www.witsml.org/schemas/1series"),
new XAttribute(XNamespace.Xmlns.GetName("xsi"), "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute("version", "1.4.1.1"));
xDoc.Add(root);
var trajectory = new XElement("trajectory",
new XAttribute("uidWell", "b213fb5d-d203-41e3-8d03-64d71d5d19c2"),
new XAttribute("uidWellbore", "c8ede8d3-7838-4287-aad2-3da717150dff"),
new XAttribute("uid", "31EXX40"));
trajectory.Name = trajectory.Name.LocalName; // try to remove xmlns
root.Add(trajectory);
这会给我带来
```xml
<trajectorys xmlns="http://www.witsml.org/schemas/1series" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4.1.1">
<trajectory uidWell="b213fb5d-d203-41e3-8d03-64d71d5d19c2" uidWellbore="c8ede8d3-7838-4287-aad2-3da717150dff" uid="31EXX40" xmlns="">
</trajectory>
</trajectorys>
如您所见,额外的xmlns已经添加到trajectory元素中,我尝试删除它,如其他帖子中建议的那样,但没有成功。创建的XML文档将由另一个程序读取,如果不删除xmlns,它将无法正确处理。也许我的代码可以进行微调,以便不添加到trajectory元素中。
<details>
<summary>英文:</summary>
I need to create XM that looks like this.
<trajectorys xmlns="http://www.witsml.org/schemas/1series" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4.1.1">
<trajectory uidWell="b213fb5d-d203-41e3-8d03-64d71d5d19c2" uidWellbore="c8ede8d3-7838-4287-aad2-3da717150dff" uid="31EXX40">
</trajectory>
</trajectorys>
My code is.
var xDoc = new XDocument();
XNamespace ns1 = "http://www.witsml.org/schemas/1series";
XNamespace ns2 = "http://www.w3.org/2001/XMLSchema-instance";
var root = new XElement(ns1 + "trajectorys",
new XAttribute("xmlns", "http://www.witsml.org/schemas/1series"),
new XAttribute(XNamespace.Xmlns.GetName("xsi"), "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute("version", "1.4.1.1"));
xDoc.Add(root);
var trajectory = new XElement("trajectory",
new XAttribute("uidWell", "b213fb5d-d203-41e3-8d03-64d71d5d19c2"),
new XAttribute("uidWellbore", "c8ede8d3-7838-4287-aad2-3da717150dff"),
new XAttribute("uid", "31EXX40"));
trajectory.Name = trajectory.Name.LocalName; // try to remove xmlns
root.Add(trajectory);
Which gives me
```xml
<trajectorys xmlns="http://www.witsml.org/schemas/1series" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4.1.1">
<trajectory uidWell="b213fb5d-d203-41e3-8d03-64d71d5d19c2" uidWellbore="c8ede8d3-7838-4287-aad2-3da717150dff" uid="31EXX40" xmlns="">
</trajectory>
</trajectorys>
As you can see, an extra xmlns has been added to the trajectory element, and my attempt to remove it, as recommended in other posts, has failed. The XML document that is created is read by another program, which will fail to process it properly if the xmlns is not removed. Perhaps my code can be tweaked so that itis not added to the trajectory element.
答案1
得分: 0
你需要在所需的命名空间中创建<trajectory>
元素。XElement
不会继承其父元素的命名空间,因此,如果您在不指定命名空间的情况下创建它,它将位于空命名空间中。
因此,如果您将您的代码修改如下:
var trajectory = new XElement(root.Name.Namespace + "trajectory", // 在与根相同的命名空间中创建
new XAttribute("uidWell", "b213fb5d-d203-41e3-8d03-64d71d5d19c2"),
new XAttribute("uidWellbore", "c8ede8d3-7838-4287-aad2-3da717150dff"),
new XAttribute("uid", "31EXX40"));
您将得到:
<trajectorys xmlns="http://www.witsml.org/schemas/1series" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4.1.1">
<trajectory uidWell="b213fb5d-d203-41e3-8d03-64d71d5d19c2" uidWellbore="c8ede8d3-7838-4287-aad2-3da717150dff" uid="31EXX40" />
</trajectorys>
这与您所需的 XML 在语义上是相同的。但请注意,<trajectory />
是自闭合的,但您所需的 XML 显示它具有单独的开启和关闭标签。如果出于某种原因您需要这样,可以指定一个空值:
var trajectory = new XElement(root.Name.Namespace + "trajectory", // 在与根相同的命名空间中创建
"", // 空值以强制具有不同的开启和关闭标签
new XAttribute("uidWell", "b213fb5d-d203-41e3-8d03-64d71d5d19c2"),
new XAttribute("uidWellbore", "c8ede8d3-7838-4287-aad2-3da717150dff"),
new XAttribute("uid", "31EXX40"));
trajectory.Name = trajectory.Name.LocalName;
这一行是不必要的。
演示代码 在这里。
英文:
You need to create the <trajectory>
element in the required namespace. An XElement
will not inherit its namespace from its parent, so if you create it without specifying a namespace, it will go into the empty namespace.
Thus if you modify your code as follows:
var trajectory = new XElement(root.Name.Namespace + "trajectory", // Create it in the same namespace as the root
new XAttribute("uidWell", "b213fb5d-d203-41e3-8d03-64d71d5d19c2"),
new XAttribute("uidWellbore", "c8ede8d3-7838-4287-aad2-3da717150dff"),
new XAttribute("uid", "31EXX40"));
You will get
<trajectorys xmlns="http://www.witsml.org/schemas/1series" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4.1.1">
<trajectory uidWell="b213fb5d-d203-41e3-8d03-64d71d5d19c2" uidWellbore="c8ede8d3-7838-4287-aad2-3da717150dff" uid="31EXX40" />
</trajectorys>
Which is semantically identical to your required XML. Note however that the <trajectory />
is self closing but your required XML shows it to have separate opening and closing tags. If you require that for some reason, specify an empty value:
var trajectory = new XElement(root.Name.Namespace + "trajectory", // Create it in the same namespace as the root
"", // Empty value to force distinct opening and closing tags
new XAttribute("uidWell", "b213fb5d-d203-41e3-8d03-64d71d5d19c2"),
new XAttribute("uidWellbore", "c8ede8d3-7838-4287-aad2-3da717150dff"),
new XAttribute("uid", "31EXX40"));
The trajectory.Name = trajectory.Name.LocalName;
line is unnecessary.
Demo fiddle here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论