1

I have an xml Template that I use to store values into a db (via a web-service). I have seen examples of how to update an xml String using linq. For example...

<Contacts>
    <Contact>
        <FirstName>Petar</FirstName>
        <LastName>Petrovic</LastName>
        <Email>[email protected]</Email>
        <Address>Pere Perica 10</Address>
        <ZipCode>1000</ZipCode>
        <City>Belgrade</City>
        <State>Serbia</State>
    </Contact>
</Contacts>

If this was an xml doc you wanted to update, You would simply do something

XElement xmlDoc = new XElement("Contacts", 
                  from c in db.Contacts
                  orderby c.ContactID
                  select new XElement("Contact",
                         new XElement("ContactID", c.ContactID),
                         new XElement("FirstName", c.FirstName),
                         new XElement("LastName", c.LastName)));
xmlDoc.Save(Server.MapPath(@"~/export.xml"));

Which is pretty cool. But I would need to update nodes that are essentially the same except for their attributes. For example...

<?xml version="1.0" encoding="utf-8"?>
<dataTemplateSpecification id="id1" name="name1">
  <description>
    <html>text</html>
  </description>
  <templates>
    <template>
      <elements>
        <element id="element0" name="PatientId" display="Patient ID" dataType="String"  visable="true" readOnly="false">
        </element>
        <element id="element1" name="EMPIID" display="EMPI ID" dataType="String" visable="true" readOnly="true">
        </element>
      </elements>
      <dataTypeSpecifications>
        <dataTypeSpecification id="" baseType="KeyValuePair">
          <dictionaryDefinition>
            <item key="-1" value="-SELECT-" />
            <item key="1" value="YES" />
            <item key="0" value="NO" />
          </dictionaryDefinition>
        </dataTypeSpecification>
      </dataTypeSpecifications>

You see, I have similar nodes that are differentiated by their attributes, namely, the name attributes... as well as the value attributes... How would I use linq to update that? I am thinking I would select a new Xelement using kind of xPath type stuff where i would select the element by the name, and then just set that value? But I am a little confused about how to do that. Any ideas?

1 Answer 1

1

As you suggested you can select an element (attribute) by name, and assign a new value. I have altered your first element with this.

        var xml = XElement.Load("XMLFile1.xml");

        var node = xml.Descendants("element").Where(e => e.Attribute("id").Value == "element0").FirstOrDefault();
        if (node != null)
        {
            node.Attribute("name").Value = "something else";
        }
Sign up to request clarification or add additional context in comments.

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.