C# 筛选属性名称的 XML



这是我的XML文件

<SW.Blocks.CompileUnit ID="3" CompositionName="CompileUnits">
<AttributeList>
<NetworkSource>
<FlgNet xmlns="http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v2">
<Parts>
<Access Scope="GlobalVariable" UId="21">
<Symbol>
<Component Name="PlantConfigData" />
<Component Name="C001" />
<Component Name="command" />
<Component Name="conveyorGUID" />
</Symbol>
</Access>
<Access Scope="GlobalVariable" UId="22">
<Symbol>
<Component Name="PlantConfigData" />
<Component Name="C001" />
</Symbol>
</Access>
<Call UId="23">
<CallInfo Name="Conveyor Type C" BlockType="FB">
<Instance Scope="GlobalVariable" UId="24">
<Component Name="Conveyor Type C_DB" />
</Instance>
<Parameter Name="GUID" Section="Input" Type="String" />
<Parameter Name="Auto_Hand" Section="Input" Type="Bool" />
<Parameter Name="Notaus" Section="Input" Type="Bool" />
<Parameter Name="Input" Section="Input" Type="typeConveyorDrive" />
<Parameter Name="out1" Section="Output" Type="Bool" />
</CallInfo>
</Call>
</Parts>
<Wires>
<Wire UId="29">
<OpenCon UId="25" />
<NameCon UId="23" Name="en" />
</Wire>
<Wire UId="30">
<IdentCon UId="21" />
<NameCon UId="23" Name="GUID" />
</Wire>
<Wire UId="31">
<OpenCon UId="26" />
<NameCon UId="23" Name="Auto_Hand" />
</Wire>
<Wire UId="32">
<OpenCon UId="27" />
<NameCon UId="23" Name="Notaus" />
</Wire>
<Wire UId="33">
<IdentCon UId="22" />
<NameCon UId="23" Name="Input" />
</Wire>
<Wire UId="34">
<NameCon UId="23" Name="out1" />
<OpenCon UId="28" />
</Wire>
</Wires>
</FlgNet>
</NetworkSource>
<ProgrammingLanguage>FBD</ProgrammingLanguage>
</AttributeList>
<ObjectList>
<MultilingualText ID="4" CompositionName="Comment">
<ObjectList>
<MultilingualTextItem ID="5" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text>Bausteinaufruf C001 GUID?</Text>
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
<MultilingualText ID="6" CompositionName="Title">
<ObjectList>
<MultilingualTextItem ID="7" CompositionName="Items">
<AttributeList>
<Culture>de-DE</Culture>
<Text>C001</Text>
</AttributeList>
</MultilingualTextItem>
</ObjectList>
</MultilingualText>
</ObjectList>
</SW.Blocks.CompileUnit>

我想用 LINQ 过滤所有属性名称为"ID"(称为 XName?!(的 XElements。我不在乎价值。我需要在所有 ID 元素中编写自己的值(如 1....10,用于下一次调用 11...20(,因为它们必须是唯一的。

所以有我的主要 xml,我将调用上面的 xml,更改值,并将其复制到我的主 xml 中。取决于我的设备数量。

我看到了很多在值之后过滤的示例,但没有针对属性名称的示例。

我的代码直到知道:

var id =
from el in root.Descendants(nse_type + "SW.Blocks.CompileUnit")
where // I need to filter?
select el;

也许更好的方法是使用 LINQ 获取所有 XElements 并在 foreach(( 中过滤以更改属性名称为"ID"的值?

我是初学者,也许它没有那么复杂。谢谢!

好吧,如果要获取具有ID属性的所有元素,可以执行以下操作:

var elements= from el in root.Descendants()
where el.Attribute("ID") != null //This will check if the attribute exist or not              
select el;

现在,您可以循环访问结果以更新属性:

int i=0;
foreach (XElement e in elements)
{
e.Attribute("ID").Value=(i++).ToString();
}

就像Rui Jarimba说的那样,你应该序列化和反序列化xml, 我想你应该在这里找到你的答案:https://stackoverflow.com/a/14663848/5034209

反序列化后,可以使用 Linq To 对象、使用 for 循环并更改 ID。

最后使用序列化重新创建 xml。

唯一的行为是您的 xml 将与原始 xml 不同......

你也可以用Linq to XML来做到这一点:

解析 XML

循环访问所需的元素(使用 Linq 查询(

更改属性 ID (xml.SetAttributeValue("UId", 1((

保存 XML (xml.Save("[fileNameOrStream]"((

最新更新