Solved

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

Posted on 2014-10-22
6
437 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 33

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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