将xml数据表解析为数据集

时间:2014-10-22 16:40:34

标签: c# asp.net xml parsing datatable

我编写了一个网页,需要解析从API调用派生的XML中的数据,但XML被格式化为数据头,数据体类型架构。

我需要能够为ASP.net C#页面解析这个问题,我将把数据导入到SQL Server表中......任何帮助都将不胜感激!

有问题的XML是:

<?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>

3 个答案:

答案 0 :(得分:2)

您可以使用Linq-to-XML或XPath来解析xml,然后使用Entity Framework(或直接ADO.NET)将其插入数据库。

答案 1 :(得分:0)

如果您的目标是在HTML页面中显示此XML,则可以使用XSLT在HTML中转换XML。 这就是Windows Live Messenger用于对话日志以及IIS用于跟踪日志的内容。

在使用转换指令创建XSLT之后,您只需要将XML中的引用添加到XSLT中,并且在浏览器中打开XML时会发生魔术。

检查此示例:http://www.w3schools.com/xml/xml_xsl.asp

答案 2 :(得分:0)

我能够通过此功能解决此问题:

public DataTable parseReport(List<zapi.ClientReportResultItem> reportItems)
{
    StringBuilder sb = new StringBuilder();
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(reportItems[0].body);
    StringReader sReader = new StringReader(xmlDoc.InnerXml);
    System.Xml.Linq.XDocument d = System.Xml.Linq.XDocument.Load(sReader);
    List<_headers> headers = new List<_headers>();
    _headers header = new _headers();

    var headerXml = from r in d.Descendants("data-header")
                 .Descendants("dr")
                 .Descendants("dv")
                 select new
                 {
                     dv = r.Value,
                     dv_type = r.Attribute("type").Value
                 };
    foreach (var _val in headerXml)
    {
        header = new _headers();
        header.name = _val.dv;
        header.type = _val.dv_type;
        headers.Add(header);
    }

    //Create Data Table
    DataTable reportData = new DataTable();
    foreach (var head in headers)
    {
        if (head.type == "timestamp")
            reportData.Columns.Add(head.name, typeof(DateTime));
        else if (head.type == "int")
            reportData.Columns.Add(head.name, typeof(int));
        else if (head.type == "string")
            reportData.Columns.Add(head.name, typeof(string));
        else if (head.type == "float")
            reportData.Columns.Add(head.name, typeof(float));
        else if (head.type == "long")
            reportData.Columns.Add(head.name, typeof(long));
    }

    var data = from r in d.Descendants("data-body")
               .Descendants("dr")
               where r.HasAttributes == false
               select new
               {
                   dv = r.Elements()
               };

    foreach (var datavalue in data)
    {
        DataRow dr = reportData.NewRow();
        for (int iCount = 0; iCount < datavalue.dv.Count(); iCount++)
        {
            dr[iCount] = datavalue.dv.ToArray()[iCount].Value;
        }
        reportData.Rows.Add(dr);
    }

    return reportData;
}