c# - LINQ to XML Replace Value Issue -
i having issue when trying replace value of xelement. not sure doing wrong. simplified xml follows
<payload> <field name="slug_locator"> <value>orig value</value> </field> </payload>
here query
ienumerable<xelement> fields = el in doc.descendants("field") el.attribute("name").value == "slug_locator" select el; foreach (xelement el in fields) { e.value = "new value"; }
if check el in each statement says value = orig value. assumed change , expect see this
<payload> <field name="slug_locator"> <value>new value</value> </field> </payload>
but , dont know why
<payload> <field name="slug_locator">new value</field> </payload>
any appreciated!
currently, you're selecting <field>
element instead of <value>
element. that's why entire content of <field>
changed new value
. can modify select
part of linq slightest change fix problem :
ienumerable<xelement> values = el in doc.descendants("field") el.attribute("name").value == "slug_locator" select el.element("value");
or if need remain selecting <field>
s, change for
loop content :
foreach (xelement el in fields) { el.element("value").value = "new value"; }
Comments
Post a Comment