May i know how to update the second element’s attribute using linq to xml? I do write some code but it doesnt work, it only update the user attribute….I’m sorry for asking this kind simple question.
My XML:
<Settings>
<Settig>
<User id="1" username="Aplha"/>
<Location Nation="USA" State="Miami" />
<Email>user1@hotmail.com</Email>
</Setting>
</Settings>
My Cs :
public static void saveSetting(MainWindow main)
{
XDocument document = XDocument.Load("Setting.xml");
IEnumerable<XElement> query = from p in document.Descendants("User")
where p.Attribute("id").Value == "1"
select p;
foreach (XElement element in query)
{
string i = "New York";
element.SetAttributeValue("State", i);
}
document.Save("Setting.xml");
}
You want to select the
Settingelements; you can still select onid=1, like this:Then select the
Locationelement before updating: