C# 过滤 XML 的属性名称

标签 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>
                <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?!)的 XElement。我不关心值(value)。我需要在所有 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”的值?

我是个初学者,也许没那么复杂。非常感谢!

最佳答案

那么,如果你想获取所有具有 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();
}

关于C# 过滤 XML 的属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52857705/

相关文章:

php - 用户定义的 mysql 表的 XML 输出

swift - Select() 从 swift 3.0 中的 LINQ?

c# - Linq Navigation Properties complex where ID in (select id from...)

c# - Entity Framework : Check all relationships of an entity for foreign key use

c# - 在 ASP.NET Identity Core 中,我可以更改 UserToken 主键吗

javascript - jQuery : XML - Reading child nodes from a specific node

xml - Mule XPath Splitter 无法在第一个元素之外工作

c# - 一个组内的 LINQ-to-objects 索引 + 用于不同的分组(又名 ROW_NUMBER 与 PARTITION BY 等效)

c# - 在 ASP.NET 中连接到 SQL Server 数据库

c# - .NET CQL 解析器是否存在?