Solved

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

Posted on 2014-10-22
6
414 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 32

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 62

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 62

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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now