首页 > 解决方案 > 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初学者,也许它没有那么复杂。非常感谢!

标签: c#xmllinq

解决方案


好吧,如果要获取所有具有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();
}

推荐阅读