C#筛选属性名称的XML
这是我的xml文件C#筛选属性名称的XML,c#,xml,linq,C#,Xml,Linq,这是我的xml文件 <SW.Blocks.CompileUnit ID="3" CompositionName="CompileUnits"> <AttributeList> <NetworkSource> <FlgNet xmlns="http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v2"> <Parts&g
<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>
也许更好的方法是在foreach()中使用LINQ和filter获取所有像素元素,以更改属性名为“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, 我想你应该在这里找到答案: 在反序列化之后,可以使用Linq To对象、使用for循环并更改ID 最后,使用序列化来重新创建xml 唯一的行为是您的xml将不同于原始xml 还可以使用Linq到XML: 解析XML 在所需元素上循环(使用Linq查询) 更改属性Id(xml.SetAttributeValue(“UId”,1))
保存XML(XML.Save(“[fileNameOrStream]”)那么基本上您希望能够编辑XML字符串吗?一个选项是1)将其反序列化为C#对象,然后2)更改对象,然后3)序列化更改的对象。否则请使用Linq2XML,请参阅,非常感谢!这正是我想要的!你的代码有效!
int i=0;
foreach (XElement e in elements)
{
e.Attribute("ID").Value=(i++).ToString();
}