Reputation: 1813
XML File
<?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>
I have this empty xml template. I am wondering to how to update / insert value in the elements of this xml using LInq?
Thats what I am trying... needs direction...
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"
??????
Upvotes: 2
Views: 4802
Reputation: 273244
You are currently creating a list of anonymous type objects with
from i in Doc.Descendants("Section")
select new { ... }
Instead, create a list of elements to update:
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(...);
Note that XML is case-sensitive.
Upvotes: 3