1
我有一个xml模板,用于将值存储到数据库(通过Web服务)。我见过如何使用linq更新XML字符串的例子。例如...使用linq更新XML节点属性
<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>
如果这是你想更新XML文档,你只会做一些
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"));
这是很酷。但是我需要更新除了属性之外基本相同的节点。例如...
<?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>
你看,我有被它们的属性,即name属性...以及属性的值区分类似的节点......我将如何使用LINQ更新那?我想我会选择一种新的Xelement使用类型的xPath类型的东西,我会选择名称的元素,然后只是设置该值?但我对如何做到这一点有些困惑。有任何想法吗?