如何从元素中删除xmlns或在创建时避免添加它

huangapple go评论68阅读模式
英文:

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.

    &lt;trajectorys xmlns=&quot;http://www.witsml.org/schemas/1series&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; version=&quot;1.4.1.1&quot;&gt;
      &lt;trajectory uidWell=&quot;b213fb5d-d203-41e3-8d03-64d71d5d19c2&quot; uidWellbore=&quot;c8ede8d3-7838-4287-aad2-3da717150dff&quot; uid=&quot;31EXX40&quot;&gt;
      &lt;/trajectory&gt;
    &lt;/trajectorys&gt;


My code is.

    var xDoc = new XDocument();
    XNamespace ns1 = &quot;http://www.witsml.org/schemas/1series&quot;;
    XNamespace ns2 = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;;
    var root = new XElement(ns1 + &quot;trajectorys&quot;,
        new XAttribute(&quot;xmlns&quot;, &quot;http://www.witsml.org/schemas/1series&quot;),
        new XAttribute(XNamespace.Xmlns.GetName(&quot;xsi&quot;), &quot;http://www.w3.org/2001/XMLSchema-instance&quot;),
        new XAttribute(&quot;version&quot;, &quot;1.4.1.1&quot;));

    xDoc.Add(root);

    var trajectory = new XElement(&quot;trajectory&quot;,
        new XAttribute(&quot;uidWell&quot;, &quot;b213fb5d-d203-41e3-8d03-64d71d5d19c2&quot;),
        new XAttribute(&quot;uidWellbore&quot;, &quot;c8ede8d3-7838-4287-aad2-3da717150dff&quot;),
        new XAttribute(&quot;uid&quot;, &quot;31EXX40&quot;));

    trajectory.Name = trajectory.Name.LocalName;    // try to remove xmlns
    root.Add(trajectory);


Which gives me

```xml
&lt;trajectorys xmlns=&quot;http://www.witsml.org/schemas/1series&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; version=&quot;1.4.1.1&quot;&gt;
  &lt;trajectory uidWell=&quot;b213fb5d-d203-41e3-8d03-64d71d5d19c2&quot; uidWellbore=&quot;c8ede8d3-7838-4287-aad2-3da717150dff&quot; uid=&quot;31EXX40&quot; xmlns=&quot;&quot;&gt;
  &lt;/trajectory&gt;
&lt;/trajectorys&gt;

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 &lt;trajectory&gt; 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 + &quot;trajectory&quot;, // Create it in the same namespace as the root
	new XAttribute(&quot;uidWell&quot;, &quot;b213fb5d-d203-41e3-8d03-64d71d5d19c2&quot;),
	new XAttribute(&quot;uidWellbore&quot;, &quot;c8ede8d3-7838-4287-aad2-3da717150dff&quot;),
	new XAttribute(&quot;uid&quot;, &quot;31EXX40&quot;));

You will get

&lt;trajectorys xmlns=&quot;http://www.witsml.org/schemas/1series&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; version=&quot;1.4.1.1&quot;&gt;
  &lt;trajectory uidWell=&quot;b213fb5d-d203-41e3-8d03-64d71d5d19c2&quot; uidWellbore=&quot;c8ede8d3-7838-4287-aad2-3da717150dff&quot; uid=&quot;31EXX40&quot; /&gt;
&lt;/trajectorys&gt;

Which is semantically identical to your required XML. Note however that the &lt;trajectory /&gt; 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 + &quot;trajectory&quot;, // Create it in the same namespace as the root
	&quot;&quot;, // Empty value to force distinct opening and closing tags
	new XAttribute(&quot;uidWell&quot;, &quot;b213fb5d-d203-41e3-8d03-64d71d5d19c2&quot;),
	new XAttribute(&quot;uidWellbore&quot;, &quot;c8ede8d3-7838-4287-aad2-3da717150dff&quot;),
	new XAttribute(&quot;uid&quot;, &quot;31EXX40&quot;));

The trajectory.Name = trajectory.Name.LocalName; line is unnecessary.

Demo fiddle here.

huangapple
  • 本文由 发表于 2023年6月5日 20:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定