Reputation: 72
I have an xml file structured like below:
<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>
The above is just a sample, the DataField tag repeats multiple time with different ids, but with the same tags and names.
I tried below. It gets the datafield with the ID I want, but instead of replaces the content of just the File tag, it replace everything between the 'ExtendedAttributes' tag.
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;
}
Its understandable that this does not do what I want because it gets the value of the 'ExtendedAttributes' tag and replaces it. Although i.Element(nameSpace + "ExtendedAttributes").Value
does return only the the value in the file
tag. I just can't figure out how to update the value in the 'File' tag and still be restricted to just the FORM_HTML datafield.
The result of that code is:
<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>
What I want is:
<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>
The Question: How would I go about replacing the element in the 'File' tag?
Upvotes: 1
Views: 35
Reputation: 617
All you need is to change the value of File Element instead of the ExtendedAttributes Element.
Change the code inside foreach loop to the following, it will update value of all the File Elements in the ExtendedAttributes Element:
i.Element(nameSpace + "ExtendedAttributes").Descendants(nameSpace + "File").ToList().ForEach(file => file.Value = "value for File");
Upvotes: 1