Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Updating XML Using Linq

Question posted by: =?Utf-8?B?VGVycmFuY2U=?= (Guest) on June 27th, 2008 07:20 PM
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?
--
TC
Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: Updating XML Using Linq
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/

=?Utf-8?B?VGVycmFuY2U=?='s Avatar
=?Utf-8?B?VGVycmFuY2U=?=
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: Updating XML Using Linq
Thanks a bunch. That worked.
--
TC


"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?
--
TC


 
Not the answer you were looking for? Post your question . . .
189,875 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors