【发布时间】:2016-03-01 02:18: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> 节点。
有没有办法做到这一点?我在网上看到的所有示例以及我从这里得到的帮助都提到了使用 Descendants(CORRECTED: using parent now),因为我想在某些根级别的后代之后添加节点。
提前致谢。
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>
【问题讨论】:
-
你能发布一个你期望的示例 XML 吗?
-
检查我对问题所做的更新。
-
还是不清楚。所以你想要
<level6><level7/><newlevel/></level6>而不是<level6><level7><newlevel/></level7></level6>? -
如果能贴两个XML就更清楚了:一个是你拥有的,一个是你想拥有的。
-
请检查我的编辑。