XML文件
<?xml version="1.0" encoding="utf-8"?>
<Section xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Information Section">
<ID></ID>
<alternateID />
<familyName></familyName>
<givenName></givenName>
<birthDate></birthDate>
<age></age>
<height />
<weight />
<sex></sex>
<Address>
<street1 />
<street2 />
<city />
<state />
<zipCode />
<country />
</Address>
</Section>
我有这个空的xml模板。我想知道如何使用LInq更新/插入此xml元素中的值?
这就是我想要的......需要方向......
var Doc = XDocument.Load("Info.xml");
var items = from i in Doc.Descendants("Section")
select new
{
ID = p.Element("ID").Value
}
foreach (var item in items)
item.id = "VALUE"
??????
答案 0 :(得分:3)
您当前正在使用
创建匿名类型对象列表 from i in Doc.Descendants("Section")
select new { ... }
而是创建要更新的元素列表:
var items = from i in Doc.Descendants("Section")
select i;
foreach (var item in items)
{
item.Element("ID").Value = "VALUE";
item.Element("Foo").Value = "Foo";
}
Doc.Save(...);
请注意,XML区分大小写。