I'm trying to update an xml file but seem to be having problems. My xml file
is in the following format:
<main>
<site name="mysite" username="user" password="pass" />
<site ... />
</main>
The code I have is:
XDocument mydoc =
XDocument.Load(this.txtFilePath.Text,LoadOptions.PreserveWhitespace);
XElement myElem = mydoc.Elements("site").Where(e => (string)
e.Attribute("name") == fieldArray[0]).FirstOrDefault();
if (myElem == null) Console.WriteLine("myElem is null");
//myElem.SetAttributeValue("name",this.txtSite.Text);
myElem.Attribute("name").Value = this.txtSite.Text;
//myElem.SetAttributeValue("login", this.txtUsername.Text);
myElem.Attribute("login").Value = this.txtUsername.Text;
//myElem.SetAttributeValue("password", this.txtPassword.Text);
myElem.Attribute("password").Value = this.txtPassword.Text;
It doesn't seem to be working. myElem variable seems to be null. Any
suggestions?

Signature
TC
Martin Honnen - 23 May 2008 15:07 GMT
> I'm trying to update an xml file but seem to be having problems. My xml file
> is in the following format:
[quoted text clipped - 20 lines]
> It doesn't seem to be working. myElem variable seems to be null. Any
> suggestions?
The 'site' elements are descendants of the XDocument node so either use
mydoc.Descendants("site")
or access the root element first
mydoc.Root.Elements("site")

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Terrance - 23 May 2008 18:20 GMT
Thanks a bunch. That worked.

Signature
TC
> I'm trying to update an xml file but seem to be having problems. My xml file
> is in the following format:
[quoted text clipped - 20 lines]
> It doesn't seem to be working. myElem variable seems to be null. Any
> suggestions?