CSharp/C# >> Looking for samples of parsing xml with linq dump to database table

by GiJeet » Thu, 21 May 2009 01:19:26 GMT

Hello, I get a fairly large xml file from a vendor and I need to parse
it and dump it into a database table via passing the contents to
stored procedure parameters. I would like to use linq. I'm rather new
to linq and I'm looking for some samples that illustrate this. I can
find many linq examples but I'm looking for some that parse out the
xml file in a loop and calling a stored proc.

TIA

CSharp/C# >> Looking for samples of parsing xml with linq dump to database table

by Martin Honnen » Thu, 21 May 2009 01:44:47 GMT



What do you need help with, using LINQ to XML to extract the data from
your XML document and/or using LINQ to SQL to call a stored procedure?


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

CSharp/C# >> Looking for samples of parsing xml with linq dump to database table

by GiJeet » Thu, 21 May 2009 01:49:17 GMT


Parsing the xml with linq.

CSharp/C# >> Looking for samples of parsing xml with linq dump to database table

by Martin Honnen » Thu, 21 May 2009 02:07:52 GMT


If you need concrete help then you need to show a representative sample
of your XML and explain which data you want to extract.

Here is a very simple example. Assume we have the XML

<root>
<foo>
<bar>1</bar>
<baz>a</baz>
</foo>
<foo>
<bar>2</bar>
<baz>b</baz>
</foo>
</root>

and for each 'foo' element we want to store the 'bar' content as an int
and the 'baz' content as a string:

XDocument doc = XDocument.Load(@"..\..\XMLFile2.xml");
foreach (XElement foo in doc.Root.Elements("foo"))
{
Dump((int)foo.Element("bar"), (string)foo.Element("baz"));
}

where then the method Dump would store its arguments in the database.


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

CSharp/C# >> Looking for samples of parsing xml with linq dump to database table

by GiJeet » Fri, 22 May 2009 06:33:11 GMT


Thanks!

Similar Threads

1. Looking for sample XML to sample XSD

2. Creating a Database using the STL: Newbie looking for code samples

3. 3.0 SDK? (want to look at LINQ samples) - .Net Framework

4. 3.0 SDK? (want to look at LINQ samples)

Good morning!

Can anyone point me at where to download the 3.0 sdk (does that exist yet?)

I see the 3.0 runtime download, don't see an sdk though?  I want to take a 
look at LINQ.

Thanks in advance!

Derrick 


5. Speed of switch/case vs hash table for parsing XML - CSharp/C#

6. Dump SQL Server database to XML through C#

Ok, I'm trying to dump an SQL Server 2005 database to XML via C#, and
I'm having some troubles.  I'm relatively new to the whole .net thing,
so there may be some stupid/basic questions/assumptions involved.

The first way I thought about doing it was dumping the entire dataset
to XML.  However, as far as I can tell there's no way to populate the
entire dataset at once (I'm using an auto-generated dataset from Visual
Studio 2005).  I think if I could do that it would be simplest, but I
haven't even found something hinting that its possible.

Right now I've got a partial solution where I loop through each of the
tables and print that out as XML.  However, that is printing them out
as individual entities, instead of printing out the document as one
entire XML document.  While that might work, I'd much rather have it
all nice and together.
<database><table1>...</table1><table2>...</table2>... is what I'd like.
 What I'm getting is
<document><table1>...</table1></document><document><table2>...</table2></document>...

7. Dump SQL CE Database into dataset XML - SQL Server CE

8. Fill Database Table with Sample data

I want to load some sample data for my nunit unit tests in the database. 
And I want to do this by taking the data from a file, preferably an xml file that is generated by the command "SELECT * FROM SomeTbl FOR XML AUTO". I want to load this back to the table without much effort. 
Is there a simple way of doing this?
Is there any adapter that lets me fill the database table using the xml file?

Thanks