【问题标题】:How to update an element located multiple level deep inside an xml file如何更新位于 xml 文件内部多层深处的元素
【发布时间】:2019-08-28 20:46:55
【问题描述】:

我有一个如下结构的 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


    【解决方案1】:

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

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

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

    【讨论】:

    • 这会导致“System.NullReferenceException”。它'i.Element(nameSpace + "ExtendedAttributes").Element(nameSpace + "File").Value;'返回空
    • 更新的代码 sn-p 解决了这个问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多