我有以下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>
节点。
有办法吗?我在网上看到的所有例子以及我从这里得到的帮助都提到使用后代(已更正:现在使用父级),因为我想在一些根级别的后代之后添加节点。
提前致谢。
<?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>
答案 0 :(得分:1)
问题是您的查询返回给定名称的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
答案 1 :(得分:1)
你应该删除这一行:
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")));
}