?
Solved

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

Posted on 2014-10-22
6
Medium Priority
?
479 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 35

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
Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

 

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 1000 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 35

Accepted Solution

by:
ste5an earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

801 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