I want to modfiy an XML file and know next to nothing about XML, see the following snippet. I was handed some code using linq so I'm trying to utilize that.
<?xml version="1.0" encoding="UTF-8"?>
<RadanCompoundDocument xmlns="http://www.radan.com/ns/rcd">
<RadanAttributes>
<Group class="custom" name="Manufacturing" desc="These attributes are the manufacturing properties of the file."
ord="6">
<Attr num="119" name="Material" desc="Material." type="s" ord="1">
<Valid perm="e" max="20"/>
</Attr>
<Attr num="120" name="Thickness" desc="Thickness." type="r" ord="2">
<Valid perm="e" min="0" max="99999"/>
....
I want to set a value for the line containing "Attrib num="119", as shown below:
<Attr num="119" name="Material" desc="Material." type="s" ord="1" value="Material1">
<Valid perm="e" max="20"/>
</Attr>
I am using the following code in c# to search for the correct attribute and set the value:
XDocument symDoc = XDocument.Load(SymFilePath);
XElement temp = symDoc.Descendants(symNameSpace + ATT_ELEMENT)
.Where(t => t.Attribute(NUM_ATTRIBUTE).Value == MATERIAL_ATT_NUMBER).FirstOrDefault();
temp.SetValue(MaterialName)
This works to set the value, but then I lose the last part of my XML ("Valid perm="e" max="20"/>"). The attrib 119 line now shows as below:
<Attr num="119" name="Material" desc="Material." type="s" ord="1">Steel, Mild</Attr>
So my question is this: how can I set this value without losing the last part of my XML?
So after doing some more research I believe what I want to do is just add another attribute to the existing element without losing the child element. Thanks for the answers so far but I was misleading on my original question, I don't think they apply to what I'm trying to do.
Sorry for all the confusion, the first answer did what I needed to do anyways.