Advertisement

05.13.2008 at 05:33AM PDT, ID: 23397440
[x]
Attachment Details

How to use LINQ to import XML to database having a hierarchy

Asked by Dan-Aspitel in Language Integrated Query - LINQ

Tags: .NET LINQ

How can I use LINQ to read the XML and populate the database?

I have XML which contains data in a 1..n hierarchy.  The database has a foreign key relationship defined between the two tables.  

Here is a simple example with XML and a DB:
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
<root>
  <Person>
    <Name>John</Name>
    <Asset>
      <Description>John's first asset</Description>
    </Asset>
    <Asset>
      <Description>John's second asset</Description>
    </Asset>
    <Asset>
      <Description>John's third asset</Description>
    </Asset>
  </Person>
  <Person>
    <Name>Mary</Name>
    <Asset>
      <Description>Mary's first asset</Description>
    </Asset>
    <Asset>
      <Description>Mary's second asset</Description>
    </Asset>
  </Person>
</root>
 
 
 
USE [mytestDB]
GO
/****** Object:  Table [dbo].[People] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[People](
	[PersonID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nchar](50) NOT NULL,
 CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED 
(
	[PersonID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
/****** Object:  Table [dbo].[Assets] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Assets](
	[AssetID] [int] IDENTITY(1,1) NOT NULL,
	[PersonID] [int] NOT NULL,
	[AssetDescription] [nchar](200) NOT NULL,
 CONSTRAINT [PK_Assets] PRIMARY KEY CLUSTERED 
(
	[AssetID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
ALTER TABLE [dbo].[Assets]  WITH CHECK ADD  CONSTRAINT [FK_Assets_People] FOREIGN KEY([PersonID])
REFERENCES [dbo].[People] ([PersonID])
GO
ALTER TABLE [dbo].[Assets] CHECK CONSTRAINT [FK_Assets_People]
[+][-]05.13.2008 at 06:31PM PDT, ID: 21560576

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2008 at 06:45PM PDT, ID: 21560630

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Language Integrated Query - LINQ
Tags: .NET LINQ
Sign Up Now!
Solution Provided By: CarlVerret
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.07.2008 at 01:41AM PDT, ID: 21734757

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628