0

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?

Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • Use the debugger to check what is null. – SLaks Nov 13 '16 at 16:37
  • There is no point in writing `from c in blah select c`. – SLaks Nov 13 '16 at 16:37
  • I took it from there :http://stackoverflow.com/questions/367730/how-to-change-xml-attribute Using debugger, everything has a value when the error occurs. But the var account has all the xml file as a value – Itération 122442 Nov 13 '16 at 16:40
  • 1
    `` is not an attribute, also `Attributes[0]` is pointing to nothing. I think you're mixing the concept of elements and attributes, and its content, here. – Caramiriel Nov 13 '16 at 16:41
  • Indeed, you were right. But then I have a question: How does this work? Do you have any source that explains it to you well, without throwing pages and pages of code at you face without any explanation? (not like that http://csharp.net-informations.com/xml/how-to-read-xml.htm) – Itération 122442 Nov 13 '16 at 20:03

1 Answers1

0

You could do it like this:

foreach (XElement account in query)
    account.Element("account_Name").Add(new XAttribute("account_Name", account_Name));

Being more careful you could write some code so if the attribute accountName doesn't exist you create it, otherwise you add it to the node:

var accountNameAttribute = "account_Name";
foreach (XElement account in query)
{
    var accountName = account.Element(accountNameAttribute);
    if (accountName.Attribute(accountNameAttribute) == null)
        accountName.Add(new XAttribute(accountNameAttribute, account_Name));
    else
        accountName.Attribute(accountNameAttribute).Value = account_Name;
}

Hope this helps!

Karel Tamayo
  • 3,690
  • 2
  • 24
  • 30