Solved

How do I parse the data from XML in C# into a DataTable

Posted on 2014-10-22
6
465 Views
Last Modified: 2016-02-18
I have some XML that I am having trouble parsing the data into a DataTable, or some format to import into a SQL database...

The XML is like:

<?xml version="1.0" encoding="UTF-8"?>
<report>
	<head>
		<vars>
			<var name="userId">selfservice</var>
			<var name="networkId">607</var>
			<var name="reportName">Advertiser Channel Report</var>
		</vars>
	</head>
	<body>
		<component name="">
			<component-header>Data</component-header>
			<component-body>
				<table name="">
					<table-header/>
					<table-body>
						<overview>
							<overview_item property="Execution time:">
								0.000  second(s)
							</overview_item>
							<overview_item entity="Days stat" property="Days stat:">10/21/2014 - 10/21/2014</overview_item>
							<overview_item entity="Advertiser" property="Advertiser Name:"> Like _SS</overview_item>
						</overview>
						<data>
							<data-header>
								<dr>
									<dv type="int">Sr. No.</dv>
									<dv type="int">Advertiser ID</dv>
									<dv type="string">Advertiser Name</dv>
									<dv type="int">Channel ID</dv>
									<dv type="string">Channel Name</dv>
									<dv type="long">Channel Impressions</dv>
									<dv type="timestamp">Daily</dv>
									<dv type="long">Days stat Impressions</dv>
								</dr>
							</data-header>
							<data-body>
								<dr class="total">
									<dv>46 row(s) total</dv>
									<dv/>
									<dv/>
									<dv/>
									<dv/>
									<dv>1073</dv>
									<dv/>
									<dv>1073</dv>
								</dr>
								<dr>
									<dv t="ns">1</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">22923</dv>
									<dv t="s">CS2261</dv>
									<dv t="ns">1</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">1</dv>
								</dr>
								<dr>
									<dv t="ns">2</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">22683</dv>
									<dv t="s">CS2258</dv>
									<dv t="ns">1</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">1</dv>
								</dr>
								<dr>
									<dv t="ns">3</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">12174</dv>
									<dv t="s">CR5925</dv>
									<dv t="ns">2</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">2</dv>
								</dr>
								<dr>
									<dv t="ns">4</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">26519</dv>
									<dv t="s">CS2938</dv>
									<dv t="ns">65</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">65</dv>
								</dr>
								<dr>
									<dv t="ns">5</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">22776</dv>
									<dv t="s">CS2261</dv>
									<dv t="ns">19</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">19</dv>
								</dr>
								<dr>
									<dv t="ns">6</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">2748</dv>
									<dv t="s">CN4940</dv>
									<dv t="ns">7</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">7</dv>
								</dr>
								<dr>
									<dv t="ns">7</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">4235</dv>
									<dv t="s">CN3539</dv>
									<dv t="ns">1</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">1</dv>
								</dr>
								<dr>
									<dv t="ns">8</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">25387</dv>
									<dv t="s">CS264</dv>
									<dv t="ns">3</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">3</dv>
								</dr>
								<dr>
									<dv t="ns">9</dv>
									<dv t="ns">596749</dv>
									<dv t="s">DATest3_SS</dv>
									<dv t="ns">22779</dv>
									<dv t="s">CS2261</dv>
									<dv t="ns">3</dv>
									<dv t="ns">10/21/2014</dv>
									<dv t="ns">3</dv>
								</dr>
								<dr class="total">
									<dv>46 row(s) total</dv>
									<dv/>
									<dv/>
									<dv/>
									<dv/>
									<dv>1073</dv>
									<dv/>
									<dv>1073</dv>
								</dr>
							</data-body>
							<data-footer/>
						</data>
					</table-body>
				</table>
			</component-body>
		</component>
	</body>
</report>

Open in new window


Thanks
0
Comment
Question by:adworldmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40397212
What SQL database? SQL Server? Is it a single import or a reoccuring task?
0
 

Author Comment

by:adworldmedia
ID: 40397229
Its a process that is run daily, that will import that data into a SQL Server 2008 database table...  

Once I can parse the data, I can figure out how to import into the database... just extracting the data has perplexed me...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40397563
From the XML that you will be importing the data from which, given in your question, what XML elements will constitute a row in the data table that will then be Imported into SQL server?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adworldmedia
ID: 40397649
The <data-header> for the column names; and the <data-body> for the contents.  Ignore the totals...
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40397886
Hi adworldmedia;

The following sample code should create a DataTable from the XML which you can use to populate the SQL database with.

using System.Xml.Linq;
using System.Xml.XPath;

XDocument xdoc = XDocument.Load("C:/Working Directory/Network.xml");

DataTable table = new DataTable();

// Linq to XML to build DataTable column nsmes and data types
var cResult = (from header in xdoc.Root.XPathSelectElements("./body/component/component-body/table/table-body/data/data-header/dr")
               from hr in header.Elements("dv")
               let t = hr.Attribute("type").Value
               let cn = hr.Value.Replace(" ", "")
               let type = Type.GetType((t == "int") ? "System.Int32" :
                          (t == "string") ? "System.String" :
                          (t == "long") ? "System.Int64" : "System.DateTime")
               select new
               {
                   ColumnType = type,
                   ColumnName = cn,
               }).ToList();
// Create the DataColumn for the DataTable and populate it.
foreach( var c in cResult )
{
    DataColumn dc = new DataColumn(c.ColumnName, c.ColumnType);
    table.Columns.Add(dc);
}

// Using line to XML get the rows for the data table
var result = (from node in xdoc.Root.XPathSelectElements("./body/component/component-body/table/table-body/data/data-body/dr")
              where !node.HasAttributes
              select new  object[] 
              {   
                      ((object)int.Parse(node.Elements().ElementAt(0).Value)),
                      ((object)int.Parse(node.Elements().ElementAt(1).Value)),
                      ((object)node.Elements().ElementAt(2).Value),
                      ((object)int.Parse(node.Elements().ElementAt(3).Value)),
                      ((object)node.Elements().ElementAt(4).Value),
                      ((object)long.Parse(node.Elements().ElementAt(5).Value)),
                      ((object)DateTime.Parse(node.Elements().ElementAt(6).Value)),
                      ((object)long.Parse(node.Elements().ElementAt(7).Value))
              }).ToArray();

// Populate the rows of the DataTable
foreach(var row in result )
{
    table.LoadDataRow(row, true);
}

Open in new window

0
 
LVL 34

Accepted Solution

by:
ste5an earned 250 total points
ID: 40398101
I would consider parsing it directly in T-SQL. E.g.

 
DECLARE @Data XML;

SELECT	@Data = B.BulkColumn
FROM	OPENROWSET(BULK 'c:\temp\data.xml', SINGLE_CLOB) B;

SELECT	DataHeader.value('.', 'NVARCHAR(255)') AS HeaderName,
	DataHeader.value('@type', 'NVARCHAR(255)') AS HeaderType,
	ROW_NUMBER() OVER ( ORDER BY ( SELECT 1 ) ) AS RN
FROM	@Data.nodes('/report/body/component/component-body/table/table-body/data/data-header/dr/dv') A ( DataHeader );
	
WITH Data AS
	(
		SELECT	DataBody.query('.') AS Q,
			ROW_NUMBER() OVER ( ORDER BY ( SELECT 1 ) ) AS RN
		FROM	@Data.nodes('/report/body/component/component-body/table/table-body/data/data-body/dr[not (@class = "total")]') B ( DataBody )
	
	)
	SELECT	D.RN,
		DV.value('.', 'NVARCHAR(255)') AS BodyValue,
		DV.value('@t', 'NVARCHAR(255)') AS BodyType
	FROM	Data D
		CROSS APPLY Q.nodes('/dr/dv') C ( DV );

Open in new window

0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question