1

I'm new to C# and have been working on learning database's. Currently I'm trying to import data from a XML document that has items stored as follows:

   <CityData>
        <City>Ada</City>
        <County>Olmsted</County>
        <AreaCode>507</AreaCode>
        <Founded>1900</Founded>
        <CityWebSite>www.adacity.com</CityWebSite>
        <Population>1200</Population>
        <Zipcode>56996</Zipcode>
        <ZipcodeMax>57656</ZipcodeMax>
    </CityData>

I'm trying to save each data collection to a different row in a SQL Express database. I've been able to store other information in the database, I just don't know how to do this with a XML document. The database is stored locally on my machine along with the C# program I'm writing. I have a new table with columns named and ordered like the XML documents schema. The tutorials I'm finding online are for .asp programs. Has anyone ever done this? This is a C# Forms Program.

3
  • 1
    Which version of .net are you using? I ask because LINQ can make this pretty easy. Commented Mar 12, 2012 at 18:37
  • Please don't add "C#" to the ends of your titles. That's what the tags are for. Commented Mar 12, 2012 at 18:38
  • I'm using Visual C# Studio 2008 with SQL Server Express. I'll look up LINQ now, thanks. Commented Mar 12, 2012 at 18:41

2 Answers 2

2

This will do your job. please make sure to replace filePath\fileName.xml with full path to the xml and change /dataroot/CityData (at the bottom) as per your xml. Also sql should be able to access the file.

Declare @xml XML

Select  @xml  = 
CONVERT(XML,bulkcolumn,2) FROM OPENROWSET(BULK 'filePath\fileName.xml',SINGLE_BLOB) AS X

SET ARITHABORT ON

Insert into [YourTableName] 
        (
            City,County,AreaCode,Founded,CityWebSite,[Population],Zipcode,ZipcodeMax
        )

    Select 
        P.value('City[1]','VARCHAR(100)') AS City,
        P.value('County[1]','VARCHAR(100)') AS County,
        P.value('AreaCode[1]','VARCHAR(100)') AS AreaCode,
        P.value('Founded[1]','VARCHAR(100)') AS Founded,
        P.value('CityWebSite[1]','VARCHAR(100)') AS CityWebSite,
        P.value('Population[1]','VARCHAR(100)') AS Population,
        P.value('Zipcode[1]','VARCHAR(100)') AS Zipcode,
        P.value('ZipcodeMax[1]','VARCHAR(100)') AS ZipcodeMax,

    From @xml.nodes('/dataroot/CityData') PropertyFeed(P)
Sign up to request clarification or add additional context in comments.

13 Comments

Won't this only work if the XML file only has one CityData element?
Maybe my inexperience is holding me back but I don't have access to "Declare", I can use Select, I don't think I'm missing any using statements at the top. Is there something I'm suppose to do to get Declare @xml XML?
@CFL_Jeff I am assuming that CityData as a child node of dataroot.
This is a query and should be running on sql server query window or you could convert this to a stored procedure.
@Indikaf What if there are multiple CityData elements in the file?
|
1

First, you deserialize the XML into C# objects.

Then, you serialize the data from objects into the database.

Lucky you, C# makes XML Deserialization a snap.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.