【问题标题】:C# Filter XML for Attribute NameC# 过滤 XML 的属性名称
【发布时间】:2019-03-22 07:11:43
【问题描述】:

这是我的 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”的值?

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

【问题讨论】:

  • 所以基本上您希望能够编辑您的 XML 字符串?一种选择是 1) 将其反序列化为 C# 对象,然后 2) 更改对象,然后 3) 序列化更改的对象。
  • 否则使用Linq2XML,见stackoverflow.com/questions/1487653/…

标签: c# xml linq


【解决方案1】:

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

【讨论】:

  • 非常感谢!这正是我想要的!您的代码有效!
【解决方案2】:

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

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

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

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

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

解析 XML

循环遍历你想要的元素(使用 Linq 查询)

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多