首页 > 解决方案 > 如何更新位于 xml 文件内部多层深处的元素

问题描述

我有一个结构如下的 xml 文件:

<root>
      <DataFields>
        <DataField Id="FORM_HTML" IsArray="FALSE">
          <DataType>
            <DeclaredType Id="File">
            </DeclaredType>
          </DataType>
          <InitialValue>N/A</InitialValue>
          <Description>Form</Description>
          <ExtendedAttributes>
            <ExtendedAttribute Name="FormatString" Value="">
            </ExtendedAttribute>
            <ExtendedAttribute Name="FormatCulture" Value="">
            </ExtendedAttribute>
            <ExtendedAttribute Name="FormatPrecision" Value="0">
            </ExtendedAttribute>
            <ExtendedAttribute Name="FormatTimeZone" Value="0">
            </ExtendedAttribute>
            <ExtendedAttribute Name="Visible" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="DispSearch" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="DispList" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="DispHome" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="ReadOnly" Value="Y">
            </ExtendedAttribute>
            <ExtendedAttribute Name="File">
               <File>"What I want" </File>
            </ExtendedAttribute>
          </ExtendedAttributes>
        </DataField>
       ....
       </DataFields>

</root>

以上只是一个示例,DataField 标记重复多次,具有不同的 id,但具有相同的标记和名称。

我在下面试过。它使用我想要的 ID 获取数据字段,但不是仅替换 File 标记的内容,而是替换“ExtendedAttributes”标记之间的所有内容。

var getHTMLFORM = from data in xmlFile.Descendants(nameSpace + "DataField")
                       let customcheck = data.Attribute("Id").Value
                       where customcheck == "FORM_HTML" && customcheck != null
                        select data;
foreach (var i in getHTMLFORM)
 {
    i.Element(nameSpace + "ExtendedAttributes").Value = encodedstring;
 }

可以理解的是,这并没有做我想要的,因为它获取了“ExtendedAttributes”标签的值并替换了它。虽然i.Element(nameSpace + "ExtendedAttributes").Value确实只返回file标签中的值。我只是不知道如何更新“文件”标签中的值,但仍然仅限于 FORM_HTML 数据字段。

该代码的结果是:

<root>
      <DataFields>
        <DataField Id="FORM_HTML" IsArray="FALSE">
          <DataType>
            <DeclaredType Id="File">
            </DeclaredType>
          </DataType>
          <InitialValue>N/A</InitialValue>
          <Description>Form</Description>
          <ExtendedAttributes>
                "inserted"
          </ExtendedAttributes>
        </DataField>
       ....
       </DataFields>

</root>

我想要的是:

<root>
      <DataFields>
        <DataField Id="FORM_HTML" IsArray="FALSE">
          <DataType>
            <DeclaredType Id="File">
            </DeclaredType>
          </DataType>
          <InitialValue>N/A</InitialValue>
          <Description>Form</Description>
          <ExtendedAttributes>
            <ExtendedAttribute Name="FormatString" Value="">
            </ExtendedAttribute>
            <ExtendedAttribute Name="FormatCulture" Value="">
            </ExtendedAttribute>
            <ExtendedAttribute Name="FormatPrecision" Value="0">
            </ExtendedAttribute>
            <ExtendedAttribute Name="FormatTimeZone" Value="0">
            </ExtendedAttribute>
            <ExtendedAttribute Name="Visible" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="DispSearch" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="DispList" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="DispHome" Value="N">
            </ExtendedAttribute>
            <ExtendedAttribute Name="ReadOnly" Value="Y">
            </ExtendedAttribute>
            <ExtendedAttribute Name="File">
               <File>"Inserted" </File>
            </ExtendedAttribute>
          </ExtendedAttributes>
        </DataField>
       ....
       </DataFields>

</root>

问题:我将如何替换“文件”标签中的元素?

标签: c#linq-to-xml

解决方案


您只需更改 File 元素的值,而不是 ExtendedAttributes 元素。

将 foreach 循环中的代码更改为以下内容,它将更新 ExtendedAttributes 元素中所有文件元素的值:

i.Element(nameSpace + "ExtendedAttributes").Descendants(nameSpace + "File").ToList().ForEach(file => file.Value = "value for File");


推荐阅读