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?