How to import XML file into SQL server

I kind of get a werid problem with Imporint XML into SQL server database. I have searched google up for a whole day and still can't find the resolution.

I have a XML file of an Apartment data. I need to import this file into SQL server Database.
The xml file looks like the following:
<Apartment>

<ApartmentName>LINCOLN PROPERTY COMPANY</ApartmentName>
<ApartmentPrice>799</ApartmentPrice>
<Address>
<Address1>8203 Southwestern Blvd.</Address1>
<Address2></Address2>
<City>Dallas</City>
<State>TX</State>
<Zip>75206</Zip>
<PO_Box></PO_Box>
</Address>
</Apartment>

I use ADO.Net to read the XML data into Dataset, then update to SQL Server. After I load the the dataset, I found out the dataset has two tables, One is Address table, and the other one is apartment table with two fileds ApartmentName and ApartmentPrice. However, in the SQL server database, we need to have all the address , name and price information in one table.

Except for modifying DataSet in the code to merge these two tables together column by column, is there any other easy ways to merge two tables together. It seems like Dataset has merge method while datatable does not have it.

Thanks very much.
[1543 byte] By [mujin03] at [2007-11-19 6:12:58]
# 1 Re: How to import XML file into SQL server
Best method:
1) Create stored procedure for it (with input parameter "@i_doc TEXT")
2) In that SP :

--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @i_doc

-- Execute a SELECT statement that uses the OPENXML rowset provider and insert results to table.
INSERT INTO MyTable(ApartmentName,Address1,Address2
SELECT ApartmentName , Address1,Address2
FROM OPENXML (@idoc, '/Apartment')
WITH (
ApartmentName varchar(255) 'ApartmentName',
Address1 varchar(255) 'Address/Address1'
Address2 varchar(255) 'Address/Address2'
)

--Free XML resources
EXEC sp_xml_removedocument @idoc


3) call that SP from your application (using XML text as one of parameters)

Best regards,
Krzemo.
Krzemo at 2007-11-10 3:27:34 >