将XML解析为DataTable

时间:2016-03-29 16:54:38

标签: c# .net xml

我有以下XML:

<RECORDS>
  <RECORD>
    <PROPERTY NAME="FNAME" TYPE="string"></PROPERTY>
    <PROPERTY NAME="LNAME" TYPE="string">
      <VALUE>SMITH</VALUE>
    </PROPERTY>
    <PROPERTY NAME="ZIP" TYPE="string">
      <VALUE></VALUE>
    </PROPERTY>
    <PROPERTY NAME="PHONE" TYPE="string">
      <VALUE>5551212</VALUE>
    </PROPERTY>
  </RECORD>
  <RECORD>
     ....
  </RECORD>
  <RECORD>
     ....
  </RECORD>
  <RECORD>
     ....
  </RECORD>
</RECORDS>

“RECORDS”下可能有1000个“RECORD”节点。我试图简单地将每个记录转储到数据行(或整个事物转换为数据表)。我很乐意将所有“VALUE”放入每一行的列中。在C#中有一种有效的方法吗?做一个foreach()并一次访问一个属性似乎非常慢。

我想它会涉及Row.ItemArray或XMLArray或类似的东西。

有兴趣看到一个很好的解决方案!

1 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<RECORDS>" +
                  "<RECORD>" +
                    "<PROPERTY NAME=\"FNAME\" TYPE=\"string\"></PROPERTY>" +
                    "<PROPERTY NAME=\"LNAME\" TYPE=\"string\">" +
                      "<VALUE>SMITH</VALUE>" +
                    "</PROPERTY>" +
                    "<PROPERTY NAME=\"ZIP\" TYPE=\"string\">" +
                      "<VALUE></VALUE>" +
                    "</PROPERTY>" +
                    "<PROPERTY NAME=\"PHONE\" TYPE=\"string\">" +
                      "<VALUE>5551212</VALUE>" +
                    "</PROPERTY>" +
                  "</RECORD>" +
              "</RECORDS>";

            XDocument doc = XDocument.Parse(xml);

            DataTable dt = new DataTable();

            List<XElement> properties = doc.Descendants("PROPERTY").ToList();

            //add columns to table
            foreach (XElement property in properties)
            {
                string name = property.Attribute("NAME").Value;
                string _type = property.Attribute("TYPE").Value;
                if(!dt.Columns.Contains(name))
                {
                    dt.Columns.Add(name, Type.GetType("System." + _type, false, true));
                }
            }

            //add rows to table
            foreach(XElement record in doc.Descendants("RECORDS").ToList())
            {
                DataRow newRow = dt.Rows.Add();
                foreach (XElement property in properties)
                {
                    string name = property.Attribute("NAME").Value;
                    Type  _type  = Type.GetType("System." + property.Attribute("TYPE").Value, false, true);
                    string value = property.Value;

                    switch (_type.ToString())
                    {
                        case "System.String" :
                            newRow[name] = (string)value;
                            break;
                        case "System.Int" :
                            newRow[name] = int.Parse(value);
                            break;
                    }

                }
            }
        }
    }

}