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

本文关键字:工作 节点 添加 定位 位置 XElement | 更新日期: 2023-09-27 18:36:44

我有以下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> 节点。

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

提前谢谢。

编辑1:根据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>

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

您应该删除此行:

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")));
    }

问题是您的查询返回给定名称的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