473,395 Members | 1,468 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,395 software developers and data experts.

Linq to xml question

Hi all,

I have a xml document which contains:

<users>
<user id="000000126836">
<loginname>Bart</loginname>
</user>
<user id="600000126836">
<loginname>Piet</loginname>
</user>
</users>

Now I want to retreive just one user with a certain id. I have tried this
code but it doesn't retreive anything.

public string GetLoginName(string buttonID)
{
XDocument userList = XDocument.Load("users.xml");

var query =
from users in userList.Descendants("user")
where (string)users.Attribute("id") == buttonID
select new
{
loginname = (string)users.Element("loginname"),
};

return ?????????
}

Now my question is what is wrong with my query and how can i return the
Login

Thanks in advance,

Bart

Jun 30 '08 #1
1 967
<bwrote:
Hi all,

I have a xml document which contains:

<users>
<user id="000000126836">
<loginname>Bart</loginname>
</user>
<user id="600000126836">
<loginname>Piet</loginname>
</user>
</users>
Bart
Hi Bart,

<Maxwell Smart VoiceMissed it by that much />

private void button1_Click(object sender, EventArgs e)
{
XElement users = XElement.Load(@"C:\Temp\Users.Xml");
try
{

string login = (from xe in users.Descendants("user")
where (string)xe.Attribute("id") ==
"600000126835"
select
(string)xe.Element("loginname")).Single();
textBox1.Text = login;
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}

This will give you an exception if the ID is not found that you can
then simply handle, or if you prefer you could use the SingleOrDefault
method to just return an empty string if not found.

Regards Tim.

--

Jun 30 '08 #2

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

Similar topics

28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
1
by: shapper | last post by:
Hi, I wonder, is there some tool that transforms SQL procedures to LINQ? :-) I want to use LINQ but I have so much work done in SQL that would be great to transform my SQL code to LINQ. ...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.