I am creating a configuration file editor, and am currently implementing features for updating existing data. I would like to be able to update all attributes within the file which have a specific attribute, such as updating a user name.
My XML file represents users in the following manner:
<user user="user1" ... />
<user user="user2" ... />
My present attempt looks like this:
xdoc.Descendants().Where(a => a.Attribute("user").Value == UserEditInput).FirstOrDefault().SetAttributeValue("user", NewUser);
where UserEditInput
is the name of the current user name and NewUser
is the new replacement value.
This throws a NullReferenceException
. There are a number of "user" XAttributes in the form shown above with a value equal to that of UserEditInput
. This leads me to believe I am not referencing the desired data in the correct manner, not modifying the attributes correctly, or both.
Thank you in advance for any assistance.