Question

How can I set a value to the Url XElement inside Google?

<?xml version="1.0" encoding="UTF-8"?>
<Items>
   <Item Name="Google">
       <Id/>
       <SomethingElse/>
       <Url/>
   </Item>
   <Item Name="Yahoo">
       <Id/>
       <SomethingElse/>
       <Url/>
   </Item>
</Items>    

I know how to get the Google part:

xmlDoc = XDocument.Load("file.xml");
IEnumerable<XElement> item =
from el in xmlDoc.Descendants("Item")
      where (string)el.Attribute("Name") == "Google"
      select el;  

But how to I set a value to the Url?
(To make it for example "www.google.com")

Was it helpful?

Solution

var googleItem = item.FirstOrDefault();
var googleUrl = googleItem.Element("Url");
googleUrl.Value = "http://google.com";

After that you can save changed document:

xmlDoc.Save("file.xml");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top