【问题标题】:How to Convert XML string to List?如何将 XML 字符串转换为列表?
【发布时间】:2019-12-05 02:45:58
【问题描述】:

我在 XML 字符串下面有这个,从监控 sql 表的SqlDependency 得到的 ,我想将其转换为列表或datatable

<root>
  <inserted>
    <row>
      <invoice_id>26</invoice_id>
      <invoice_type>5</invoice_type>
      <client_id>372</client_id>
      <create_date>2019-12-02T13:49:21.430</create_date>
      <status_id>1</status_id>
      <sales_rep_id>1</sales_rep_id>
      <courrier_id>12</courrier_id>
      <items_quantity>3</items_quantity>
      <sub_total>0.00</sub_total>
      <discount>0.00</discount>
      <shipping>0.00</shipping>
      <tax>0.00</tax>
      <grand_total>0.00</grand_total>
      <prepared_by>11</prepared_by>
      <current_state>PREPARED</current_state>
      <store_ID>1</store_ID>
      <extra></extra>
      <user_id>11</user_id>
      <open_date>2019-12-02T13:52:53.583</open_date>
      <prepared_date>2019-12-02T13:54:10.877</prepared_date>
      <preparing_notes></preparing_notes>
      <to_courier_date>2019-12-02T14:34:17.953</to_courier_date>
    </row>
  </inserted>

</root> 

我尝试了以下代码:

string xml = ee.Data.ToString();// XML string above
                List<string> Lst = new List<string>();
                XmlReader xr = XmlReader.Create(new StringReader(xml));
                var xMembers = from members in XElement.Load(xr).Elements() select members;

                foreach (XElement x in ee.Data.Elements("root"))
                {
                    Lst.Add (  x.Value);
                }

但将其转换为Datatable 也很累

public DataTable XML2DT(string xmlData)
        {
            StringReader theReader = new StringReader(xmlData);
            DataSet theDataSet = new DataSet();
            theDataSet.ReadXml(theReader);

            return theDataSet.Tables[0];
        }

返回错误,谁能帮我把它转换成任何可用的对象来存储新数据

【问题讨论】:

标签: c# xml


【解决方案1】:

如果你自己定义类:

public class root
{
    public row[] inserted {get;set;}
}

public class row
{
    public int invoice_id {get;set;}
    public int invoice_type {get;set;}
    .....
}

你使用 Xml-Serialization

    var ser = new XmlSerializer(typeof(root));
    var root = (root)ser.Deserialize(new StringReader( yourxmlstring ));

比你对所有 xml 进行类型安全的转换。

我假设您的行元素可能重复(是一个列表),而插入的标签是唯一的。

它不是真正的“列表或数据表”,但仍然适用于数据绑定。 除非您的物业清单每天都在变化,而且您事先并不知道。

【讨论】:

  • 谢谢你解决了我的问题var ser = new XmlSerializer(typeof(root)); var Xroot = (root)ser.Deserialize(new StringReader(ee.Data.ToString())); List&lt;string&gt; Lst = new List&lt;string&gt;(); foreach (row r in Xroot.inserted) { Lst.Add (r.invoice_id.ToString()); }
【解决方案2】:

请尝试下面的代码 sn-p。

string xmlFile = "Data.xml";
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);

【讨论】:

    【解决方案3】:

    使用 xml linq 您可以将其放入字典中:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
             static void Main(string[] args)
             {
                 XDocument doc = XDocument.Load(FILENAME);
    
                 Dictionary<string, string> dict = doc.Descendants("row").FirstOrDefault().Elements()
                     .GroupBy(x => x.Name.LocalName, y => (string)y)
                     .ToDictionary(x => x.Key, y => y.FirstOrDefault());
    
             }
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-10
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2020-06-07
      相关资源
      最近更新 更多