473,396 Members | 1,772 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.

using LINQ with DataSet and Xml file

Hello!!

I just wonder if you have any examples of creating a LINQ query between a
DataSet and an Xml file.

//Tony
Sep 15 '08 #1
1 3343
Tony Johansson wrote:
I just wonder if you have any examples of creating a LINQ query between a
DataSet and an Xml file.
Here is a sample join between a DataTable and an XDocument:

DataTable table1 = new DataTable("example");
DataColumn col1 = new DataColumn("k", typeof(int));
table1.Columns.Add(col1);
DataColumn col2 = new DataColumn("foo", typeof(string));
table1.Columns.Add(col2);
for (int i = 0; i < 5; i++)
{
DataRow row = table1.NewRow();
row[col1] = i;
row[col2] = "foo " + i.ToString();
table1.Rows.Add(row);
}

XDocument doc = new XDocument(
new XElement("root",
new XElement("bar",
new XAttribute("k", 1),
new XAttribute("bar", "bar 1")
),
new XElement("bar",
new XAttribute("k", 3),
new XAttribute("bar", "bar 3")
),
new XElement("bar",
new XAttribute("k", 10),
new XAttribute("bar", "bar 10")
)));

var query = from row in table1.AsEnumerable()
join bar in doc.Root.Elements("bar") on
row.Field<int>("k") equals (int)bar.Attribute("k")
select new { key = row.Field<int>("k"), foo =
row.Field<string>("foo"), bar = (string)bar.Attribute("bar") };
foreach (var item in query)
{
Console.WriteLine("key: {0}; foo: {1}; bar: {2}",
item.key, item.foo, item.bar);
}
Output is

key: 1; foo: foo 1; bar: bar 1
key: 3; foo: foo 3; bar: bar 3

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 15 '08 #2

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

Similar topics

4
by: cj | last post by:
Can I use LINQ in VB .Net 2008 to get to a Visual FoxPro Table? I've seen demos of it with SQL Server but they've used it with a server browser and I don't think that'd work when I need to see VFP...
4
by: Lacutas | last post by:
Hi all, I am having some problems using LINQ to access Distinct records from a Dataset. I have looked around and believe it should be as simple as added .Distinct() to my LINQ query below, though...
1
by: Lacutas | last post by:
Hi I'm having some problems getting a dynamic LINQ query to work on my DataSet. The idea is that a user selects certain criteria, and then the LINQ query filters through the dataset making the...
1
by: Frederik | last post by:
Hi all, Am I correct when stating that LINQ replaces somewhat DataTables? I have done some reading concerning LINQ, but I'm still puzzled as to whether I should use LINQ or not. My application...
5
by: Andy B | last post by:
I was just wondering, when you create dataContext methods, should you put business logic there to try and minimize pushing data through 2-3 layers of code? or should the business logic still go in...
2
by: Andy B | last post by:
Hi... I have an sql server 2005 database that has a table called TestXml in it. The table has the columns ID (identity, int) and MyXml(xml). I created a stored proc on the server to insert a row...
0
by: Marc Gravell | last post by:
I have to make a call to fetch countries and then state. Yes; as already explained, this just fetches the *next* result grid. http://msdn.microsoft.com/en-us/library/bb534218.aspx Not on an...
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...
2
by: =?Utf-8?B?anVsaW8=?= | last post by:
I am trying to use Update to save changes made on two DataGridView's to two tables (Posts & PostDetails) with a FK relationship. I say private void bUpdate_Click(object sender, EventArgs e) {...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
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,...

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.