将xml文档转换为C#中的数据表

时间:2011-12-18 18:56:25

标签: c# .net xml web-services

我正在尝试阅读一个简单的Web服务(REST)并在我的C#桌面应用程序中填充一个下拉框。我;使用.net 2.0

以下是我的网络服务返回xml

<sections type="array">
 <section>
  <name>Standing</name>
  <created-at type="datetime">2011-10-23T23:17:54+05:30</created-at>
  <updated-at type="datetime">2011-10-23T23:17:54+05:30</updated-at>
  <id type="integer">1</id>
  <status type="integer">1</status>
  <service-charge type="float">0.0</service-charge>
 </section>
 <section>
  <name>VIP</name>
  <created-at type="datetime">2011-10-30T11:27:05+05:30</created-at>
  <updated-at type="datetime">2011-10-30T11:27:05+05:30</updated-at>
  <id type="integer">2</id>
  <status type="integer">1</status>
  <service-charge type="float">10.0</service-charge>
 </section>

并在下面的代码中我试图将xml文档转换为数据表

  public DataTable getSections() {
    String url = "http://<site_url>/sections.xml";
    DataTable t = new DataTable();
    HttpHandler handle = new HttpHandler();
    StreamReader sr = handle.executeGET(url);
    String xml = "";
    while (sr.Peek() >= 0)
    {
        xml  += sr.ReadLine();
    }
    XmlDataDocument doc = new XmlDataDocument();
    doc.LoadXml(xml);
    XmlReader xmlReader = new XmlNodeReader(doc);
    DataSet ds = new DataSet();
    ds.ReadXml(xmlReader);
    t = ds.Tables[0];
    return t;
}

在最后一段我试图将它绑定到我的下拉框(cmbSections)

DataTable t = sec.getSections();
cmbSections.DataSource = t;
cmbSections.DisplayMember = "name";
cmbSections.ValueMember = "id";

但是我收到以下错误

Cannot bind to the new display member.
Parameter name: newDisplayMember

我在这里缺少什么,请帮忙,我是C#world的新手

3 个答案:

答案 0 :(得分:1)

使用扩展方法支持将XElement转换为Datatable。您可以将此方法添加到任何实用程序类中。确保该类是静态的。

public static class XElementExtensions 
{
    public static DataTable ToDataTable(this XElement element) 
    {
        DataSet ds = new DataSet();
        string rawXml = element.ToString();
        ds.ReadXml(new StringReader(rawXml));
        return ds.Tables[0];
    }


    public static DataTable ToDataTable(this IEnumerable<XElement> elements) 
    {
        return ToDataTable(new XElement("Root", elements));
    }
}

如何使用

//Add logic to store xml data in file or string & read accordingly here.
string file = Server.MapPath("~/Data.xml");
XDocument document = XDocument.Load(file);
var query = from b in document.Elements("sections").Elements("section")
                  select b;

DataTable table = query.ToDataTable();

答案 1 :(得分:1)

将XML转换为DataSet的简单方法:

        StringReader strr = new StringReader(strXML);
        XmlTextReader xtr = new XmlTextReader(strr);
        YourTypeDataSet dstest = new YourTypeDataSet();
        dstest.ReadXml(xtr);

        if (dstest.Tables.Count > 0) ...

为了正确转换,您可以替换类型DataSet来代替:             DataSet dstest = new DataSet();

答案 2 :(得分:0)

我使用以下代码,但我不确定这是否是正确的方法

(这解析上面相同的xml)

public DataTable getSections() {
        String url = "http://<site_url>/sections.xml/sections.xml";
        DataTable t = new DataTable();
        t.Columns.Add("id", typeof(String));
        t.Columns.Add("name", typeof(String));

        HttpHandler handle = new HttpHandler();
        StreamReader sr = handle.executeGET(url);
        String xml = "";
        List<String> id = new List<string>();
        List<String> name = new List<string>();

        while (sr.Peek() >= 0)
        {
            xml  += sr.ReadLine();
        }
        XmlDataDocument doc = new XmlDataDocument();
        doc.LoadXml(xml);
        XmlReader xmlReader = new XmlNodeReader(doc);
        while (xmlReader.Read()){
            if (xmlReader.IsStartElement()) {
                String b = xmlReader.Name;
                switch (xmlReader.Name) { 
                    case "sections":
                        break;
                    case "section":
                        break;
                    case "id":
                        if (xmlReader.Read())
                        {
                            id.Add(xmlReader.Value.Trim());
                        }
                        break;
                    case "name":
                        if (xmlReader.Read())
                        {
                            name.Add(xmlReader.Value.Trim());

                        }
                        break;

                }
            }
        }

        int counter = 0;

        foreach (String i in id) {
            DataRow r = t.NewRow();
            r["id"] = i;
            r["name"] = name[counter];
            t.Rows.Add(r);    
            counter += 1;
        }
        return t;
    }

感谢您的评论:D 欢迎您提出宝贵的意见