c# - 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 过滤所有 XElement,其属性名称为“ID”(称为 XName?!)。我不在乎价值。我需要在所有 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 获取所有 XElement 并在 foreach() 中过滤以更改属性名称为 "ID" 的值?
我#ma初学者,也许它没有那么复杂。非常感谢!
解决方案
好吧,如果要获取所有具有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();
}
推荐阅读
- node.js - 如何访问我的角度应用程序以重定向到节点 js 服务器使用 res.redirect() 重定向的特定 URL
- c# - Response.BinaryWrite() 方法的 Veracode 扫描 XSS 问题
- html - 内联 SVG 矩形可以自动关闭吗?
- active-directory - 将 LDAP 查询的范围扩大到 Active Directory
- node.js - 使用中间件进行 Websockets 令牌身份验证并在 node.js 中表达
- python - 使用 Buildroot 构建具有线程支持的工具链时出现问题
- css - 在 CSS 中使用元素的属性值作为选择器
- vba - 使用VBA在PPT中制作假屏保
- asynchronous - flutter setState,为什么第一个输出为空,我如何跳过输出直到它不为空?
- javascript - 有没有办法让 v-if 在 v-for 中工作以动态呈现模板以创建子表?