Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / May 2008

Tip: Looking for answers? Try searching our database.

Updating XML Using Linq

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Terrance - 23 May 2008 14:58 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.