I would like to update an xml value in a file in c#.
Here is the XML file:
<user_Name>
Florian
<account>
<account_Name/>
<number_lines/>
<line>
<line_Id/>
<line_Date/>
<line_Desc/>
<line_Value/>
</line>
</account>
</user_Name>
I tried with LINQ and I had a nullreferenceException at the line I try to change the value
Code:
public void Create_New_Account(string _path_File)
{
Console.WriteLine(_path_File);
string account_Name = "test";
XDocument xmlFile = XDocument.Load(_path_File);
var query = from c in xmlFile.Elements("user_Name").Elements("account")
select c;
Console.WriteLine(query);
foreach (XElement account in query)
{
account.Attribute("account_Name").Value = account_Name;
}
}
I also tried with XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode node = xmlDoc.SelectSingleNode("user_Name/account/account_Name");
node.Attributes[0].Value = "test";
xmlDoc.Save(xmlFile);
Same error here. I first thought I was not passing the right path, but it is the right one. I tried to use other elements in the file, still not working.
Could someone give me a tip on something I did wrong?