XElement 在特定位置添加节点未按预期工作

XElement add node on specific position not working as expected

我有以下 XML 文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<JMF SenderID="InkZone-Controller" Version="1.2">
  <Command ID="cmd.00695" Type="Resource">
    <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
      <InkZoneProfile ID="r0013" Class="Parameter" Locked="False" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
        <InkZoneProfile SignatureName="SIG1">
          <InkZoneProfile Locked="false" SheetName="S1">
            <InkZoneProfile Side="Front">
              <ColorPool Class="Parameter" DescriptiveName="Colors for the job" Status="Available" />
              <Color Name="PANTONE 647 C" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Black" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Yellow" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Magenta" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Cyan" CMYK="1 1 0 0" sRGB="0 0 1" />
            </InkZoneProfile>
            <InkZoneProfile Separation="Cyan" ZoneSettingsX="0  0,006 0" />
            <InkZoneProfile Separation="Magenta" ZoneSettingsX="0 0,031  0" />
            <InkZoneProfile Separation="Yellow" ZoneSettingsX="0 0,021  0" />
            <InkZoneProfile Separation="Black" ZoneSettingsX="0 0,005  0" />
            <InkZoneProfile Separation="PANTONE 647 C" ZoneSettingsX="0,002 0"/>
          </InkZoneProfile>
        </InkZoneProfile>
      </InkZoneProfile>
    </ResourceCmdParams>
  </Command>
</JMF>

我想在<InkZoneProfile Side="Front">之后专门添加一个节点。我为此发出以下表达式:

XElement InkZonePath = XmlDoc.Root.Descendants("InkZoneProfile").Where(z => (string)z.Attribute("Side") == "Front").SingleOrDefault();
var InkZoneProfilePosition = InkZonePath.Parent; 
if (InkZoneProfilePosition != null)
                    {
                        InkZoneProfilePosition.Add(new XElement("InkZoneProfile",
                                    new XAttribute("Separation", x.colorname),
                                    new XAttribute("ZoneSettingsX", x.colorvalues)));
                    }

不是在我指定的位置添加节点,而是在 <ColorPool Class> 节点之后添加节点 - 我需要它在 <InkZoneProfile Side="Front">

之后

我正在寻找的输出示例:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<JMF SenderID="InkZone-Controller" Version="1.2">
  <Command ID="cmd.00695" Type="Resource">
    <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
      <InkZoneProfile ID="r0013" Class="Parameter" Locked="False" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
        <InkZoneProfile SignatureName="SIG1">
          <InkZoneProfile Locked="false" SheetName="S1">
            <InkZoneProfile Side="Front">
                       <InkZoneProfile Separation="Cyan" ZoneSettingsX="0 0,005 " />
            <InkZoneProfile Separation="Magenta" ZoneSettingsX="0  0" />
            <InkZoneProfile Separation="Yellow" ZoneSettingsX="0 0,021 0" />
            <InkZoneProfile Separation="Black" ZoneSettingsX="0 0,005 " />
            <InkZoneProfile Separation="PANTONE 647 C" ZoneSettingsX="0 0,003 " />
              <ColorPool Class="Parameter" DescriptiveName="Colors for the job" Status="Available" />
              <Color Name="PANTONE 647 C" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Black" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Yellow" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Magenta" CMYK="1 1 0 0" sRGB="0 0 1" />
              <Color Name="Cyan" CMYK="1 1 0 0" sRGB="0 0 1" />
            </InkZoneProfile>
          </InkZoneProfile>
        </InkZoneProfile>
      </InkZoneProfile>
    </ResourceCmdParams>
  </Command>
</JMF>

当我调试代码时,变量,即使在建议的更改之后,var 仍然包含 <ColorPool> 节点。

有办法吗?我在网上看到的所有示例以及从此处获得的帮助都提到了使用后代(更正:现在使用父级),因为我想在某些根级别的后代之后添加节点。

提前致谢。

EDIT1:根据 Sakura 的评论更新了代码更改 - 结果 XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<JMF SenderID="InkZone-Controller" Version="1.2">
  <Command ID="cmd.00695" Type="Resource">
    <ResourceCmdParams ResourceName="InkZoneProfile" JobID="K_41">
      <InkZoneProfile ID="r0013" Class="Parameter" Locked="False" Status="Available" PartIDKeys="SignatureName SheetName Side Separation" DescriptiveName="Schieberwerte von DI" ZoneWidth="32">
        <InkZoneProfile SignatureName="SIG1">
          <InkZoneProfile Locked="false" SheetName="S1">
            <InkZoneProfile Side="Front">
              <ColorPool Class="Parameter" DescriptiveName="Colors for the job" Status="Available" />
              <Color Name="PANTONE 647 C" CMYK="1 1 0 0" sRGB="0 0 255" />
              <Color Name="Black" CMYK="1 1 0 0" sRGB="0 0 255" />
              <Color Name="Yellow" CMYK="1 1 0 0" sRGB="0 0 255" />
              <Color Name="Magenta" CMYK="1 1 0 0" sRGB="0 0 255" />
              <Color Name="Cyan" CMYK="1 1 0 0" sRGB="0 0 255" />
              <InkZoneProfile Separation="Name" ZoneSettingsX="Value" />
              <InkZoneProfile Separation="Name" ZoneSettingsX="Value" />
              <InkZoneProfile Separation="Name" ZoneSettingsX="Value" />
              <InkZoneProfile Separation="Name" ZoneSettingsX="Value" />
              <InkZoneProfile Separation="Name" ZoneSettingsX="Value" />
            </InkZoneProfile>
          </InkZoneProfile>
        </InkZoneProfile>
      </InkZoneProfile>
    </ResourceCmdParams>
  </Command>
</JMF>

问题出在您查询 returns Descendants 给定名称( 而不是 该元素本身)。您可以通过阅读 .Parent 来获取该元素的父元素。

var firstdescendant = XmlDoc.Root.Descendants("level6")
  .Where(z => (string)z.Attribute("leveltype") == "thislevel").SingleOrDefault();


var level6element = firstdescendant.Parent; // add null check to  firstdescendant (if required)

工作Demo

你应该删除这一行:

var InkZoneProfilePosition = InkZonePath.Parent;

然后将Add改为AddFirst。应该是:

    XElement InkZonePath = XmlDoc.Root.Descendants("InkZoneProfile").Where(z => (string)z.Attribute("Side") == "Front").SingleOrDefault();
    //var InkZoneProfilePosition = InkZonePath.Parent;
    if (InkZonePath != null)
    {
        InkZonePath.AddFirst(new XElement("InkZoneProfile",
                    new XAttribute("Separation", "Name"),
                    new XAttribute("ZoneSettingsX", "Value")));
    }