Terrance wrote:
Quote:
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.P reserveWhitespace);
>
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?
|
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")
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/