C# 如何更新位于xml文件中多层深处的元素
我有一个xml文件,其结构如下:C# 如何更新位于xml文件中多层深处的元素,c#,linq-to-xml,C#,Linq To Xml,我有一个xml文件,其结构如下: <root> <DataFields> <DataField Id="FORM_HTML" IsArray="FALSE"> <DataType> <DeclaredType Id="File"> </DeclaredType> </DataType>
<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>
这不符合我的要求是可以理解的,因为它获取了“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'标记中的元素?您只需更改File元素的值,而不是ExtendedAttributes元素 将foreach循环内的代码更改为以下内容,它将更新ExtendedAttributes元素中所有文件元素的值: i、 元素(命名空间+“ExtendedAttributes”)。子体(命名空间+ “File”).ToList().ForEach(File=>File.Value=“Value for File”)
这将导致“System.NullReferenceException”。它是'i.Element(名称空间+“扩展属性”).Element(名称空间+“文件”).Value;'返回null解决问题的更新代码段。非常感谢。
<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>