c# - 如何更新位于 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>
问题:我将如何替换“文件”标签中的元素?
解决方案
您只需更改 File 元素的值,而不是 ExtendedAttributes 元素。
将 foreach 循环中的代码更改为以下内容,它将更新 ExtendedAttributes 元素中所有文件元素的值:
i.Element(nameSpace + "ExtendedAttributes").Descendants(nameSpace + "File").ToList().ForEach(file => file.Value = "value for File");
推荐阅读
- python - 同时执行一个功能多次
- vb.net - 嗨伙计。我正在尝试将应用程序从 vb6 更新到 VB dot net 并且遇到了一个非常基本的问题。当然,我会在一秒钟内添加代码。我
- sql - 语法错误:应为“(”或“,”或关键字 SELECT,但在 [23:1] 获得标识符“rfm”
- php - 方法不会在其范围之外更改对象数组
- angular-material - 带有分页的Angular材料表行跨中的索引未更新
- java - WireMock 没有收到 HTTP 客户端发出的 HTTP 请求
- excel - 在excel中显示记录数
- java - 如何从 org.apache.cayenne.util.concurrentlinkedhashmap 中获取驱逐值?
- python - numpy 数组比列表快吗?
- python - 合并具有不同内部连接条件的表