将以下XML导出到Datatable [英] Export the following XML to Datatable

查看:100
本文介绍了将以下XML导出到Datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中使用HttpWebRequest获取以下XML字符串。

我的响应XML

I''m getting the following XML string using a HttpWebRequest in c#.
My response XML

<response>
    <accounts>
        <account>
            <city>Some City</city>
            <accountId>1234567</accountId>
            <companyName>My Company</companyName>
            <country>My Country</country>
            <email>myemail@yahoo.com</email>
            <enabled>1</enabled>
        </account>
        <account>
            <city>Some Other City</city>
            <accountId>0987654</accountId>
            <companyName>My Other Company</companyName>
            <country>My Country</country>
            <email>myotheremail@yahoo.com</email>
            <enabled>1</enabled>
        </account>
    </accounts>
    <total>2</total>
    <limit>20</limit>
    <offset>0</offset>
</response>



我需要将XML导出到具有以下模式的数据表中。 />


I need to export the XML to a datatable with the following schema.

accountId | companyName      | city            | country
1234567   | My Company       | Some City       | My Country
0987654   | My Other Company | Some Other City | My Country



这是我第一次使用XML,我很困惑如何做到这一点。请帮帮我。


This is my first work with XML and I''m confused how to do it. Please help me.

推荐答案

// Forming a DataTable
dt = new DataTable("Accountdata"); // System.Data.DataTable
dt.Columns.Add("Account ID");
dt.Columns.Add("Company Name");
dt.Columns.Add("City");
dt.Columns.Add("Country");
dt.Columns.Add("Email");
dt.Columns.Add("Enabled");

XmlDocument XMLDoc = new XmlDocument(); // System.Xml.XmlDocument
Stream responseStream = response.GetResponseStream(); // "response" is the HttpWebResponse object that holds the response xml of the request
XMLDoc.Load(responseStream);
foreach (XmlNode AccData in XMLDoc.SelectNodes("/response/accounts/account"))
{
    if (AccData["accountId"] == null)
    {
        continue;
    }
    else
    {
        // Fill the DataTable line by line
        int AccountId = Convert.ToInt32(AccData["accountId"].InnerText);
        string CompanyName = AccData["companyName"].InnerText;
        string City = AccData["city"].InnerText;
        string Country = AccData["country"].InnerText;
        string Email = AccData["email"].InnerText;
        int Enabled = Convert.ToInt32(AccData["enabled"].InnerText);
        dt.Rows.Add(AccountId, CompanyName, City, Country, Email, Enabled);
    }
}




使用这段代码:



Hi,
Use this piece of code:

DataSet ds = new DataSet();
       DataTable dt = null;
       ds.ReadXml("Xml File Path");
       if(ds.Tables[0]!=null)
       {
           dt=new DataTable();
           dt = ds.Tables[0];
       }


使用您需要的列创建数据表,然后通过Linq-to-XML填充它。您可以使用Select查询创建表示每一行的对象,然后使用标准方法为每个项目创建数据行。



下面有大量详细信息开始使用..

http://support.microsoft.com/kb/311566 [ ^ ]
Create a Datatable with the columns that you require, then populate it via Linq-to-XML. You can use a Select query to create an object that represents each row, then use the standard approach for creating datarows for each item.

The below has plenty of details to get you started..
http://support.microsoft.com/kb/311566[^]


这篇关于将以下XML导出到Datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆