1

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.

3
  • Can you change the expected format of your XML? Typically you want each element to either contain elements or text, but not a combination of each. Commented Jul 17, 2014 at 16:24
  • you are setting the value for <Attr num="119" name="Material" desc="Material." type="s" ord="1"> </Attr> That is everything between the opening and closing tag. How do you want the XML to look when you are done? Commented Jul 17, 2014 at 16:33
  • CLARIFICATION: temp.SetValue is not working the way I want it to. All I want to do is add the text 'value="Material1"' at the end of the existing text. (As my second code snippet shows) Commented Jul 17, 2014 at 17:24

2 Answers 2

1

Use

temp.SetAttributeValue("value", MaterialName);

If you want to have it as XML element value you will lose the inner element 'Valid'. You cannot have both inner text and inner XML element.

Sign up to request clarification or add additional context in comments.

Comments

0

Intead of .SetValue() try using .Add() or .AddFirst():

temp.AddFirst(MaterialName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.