Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

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

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
adworldmedia
Asked:
adworldmedia
  • 2
  • 2
  • 2
2 Solutions
 
ste5anSenior DeveloperCommented:
What SQL database? SQL Server? Is it a single import or a reoccuring task?
0
 
adworldmediaCTOAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
Independent Software Vendors: 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!

 
adworldmediaCTOAuthor Commented:
The <data-header> for the column names; and the <data-body> for the contents.  Ignore the totals...
0
 
Fernando SotoRetiredCommented:
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
 
ste5anSenior DeveloperCommented:
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

Technology Partners: 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!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now