473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Updating XML Using Linq

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
Jun 27 '08 #1
2 3147
Terrance wrote:
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/
Jun 27 '08 #2
Thanks a bunch. That worked.
--
TC
"Terrance" wrote:
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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: martin | last post by:
I'm just wondering... Will it be possible to use C# 3.0 and Linq to objects without having our users download the .NET Framework 2.0 client? I really would like to use the C# 3.0 features, but I...
2
by: huwfriedhoff | last post by:
Hi Guys, I have a friend who is developing a dvd store in Visual basic ( It's a WPF application) and stores the data in an XML file. the content of the XML look like this: <DVDInfo> <DVD>...
8
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
5
by: =?Utf-8?B?U2llZ2ZyaWVkIEhlaW50emU=?= | last post by:
Can someone help me convert this to the latest C# syntax using predicate/delegate? You can see my attempt in the comments. Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
2
by: Neil Chambers | last post by:
All, I have a class describing various actions to take against a LINQ to SQL datasource. What are the pros/cons of instantiating the LINQ object either in the root of the class (for lack of a...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
0
by: wzvh3c | last post by:
I have created a DataSet from a Database. I perform a LINQ query and get a subset of the data I am interested in and that data is in an array, the resulting array is bound to a grid. If I make a...
5
by: CSharper | last post by:
I have a hashtable which has key and value. If the key is set to 'a' then I need to create a class1 and if it has key 'b' then I need to create class2 etc. (There is no name relation between the...
1
balabaster
by: balabaster | last post by:
Does anyone have experience integrating LINQ into SOA architecture. I've spent all night trawling through documents and books regarding this and I've come to the conclusion that I'm going about...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.