将XML数据集数据表合并为一个数据表

时间:2018-03-02 20:05:43

标签: c# xml datatable ssis

我需要使用C#解析XML文档以将数据导入数据库。

目前,我正在使用SSIS和此C#代码将文档读入数据集:

var xmlString = File.ReadAllText(Variables.filepath);
var stringReader = new StringReader(xmlString);
var dsSet = new DataSet();
dsSet.ReadXml(stringReader);

这完全读取了XML,但它将其分解为多个具有关系的表。有没有办法将所有数据表合并到一个表中,这样我就可以将所有数据表都放入SQL表中了?

XML示例:

XML

关系示例:

Relationship

1 个答案:

答案 0 :(得分:2)

请尝试以下操作:

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
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Message Id", typeof(string));
            dt.Columns.Add("Protocol Version", typeof(string));
            dt.Columns.Add("Name Instance URL", typeof(string));
            dt.Columns.Add("Classname", typeof(string));
            dt.Columns.Add("Key Binding", typeof(string));
            dt.Columns.Add("Key Value", typeof(string));
            dt.Columns.Add("Property Name", typeof(string));
            dt.Columns.Add("Value", typeof(string));
            dt.Columns.Add("Display Value", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);

            XElement cim = doc.Root;
            XElement message = cim.Element("MESSAGE");
            List<XElement> values = message.Descendants("VALUE.NAMEDINSTANCE").ToList();


            string messageId = (string)message.Attribute("ID");
            string version = (string)message.Attribute("PROTOCOLVERSION");

            foreach (XElement value in values)
            {
                string url = value.GetNamespaceOfPrefix("fo").NamespaceName;

                XElement instanceName = value.Element("INSTANCENAME");
                string classname = (string)instanceName.Attribute("CLASSNAME");
                string binding = (string)instanceName.Element("KEYBINDING").Attribute("NAME");
                string key = (string)instanceName.Descendants("KEYVALUE").FirstOrDefault();
                XElement instance = value.Element("INSTANCE");
                foreach (XElement valueArray in instance.Descendants("VALUE.ARRAY"))
                {
                    dt.Rows.Add(new object[] {
                        messageId,
                        version,
                        url,
                        classname,
                        binding,
                        key,
                        (string)valueArray.Attribute("NAME"),
                        (string)valueArray.Element("VALUE"),
                        (string)valueArray.Element("DisplayValue")
                    });
                }
                foreach (XElement property in instance.Elements("PROPERTY"))
                {
                    dt.Rows.Add(new object[] {
                        messageId,
                        version,
                        url,
                        classname,
                        binding,
                        key,
                        (string)property.Attribute("NAME"),
                        (string)property.Element("VALUE"),
                        (string)property.Element("DisplayValue")
                    });

                }
            }
        }
    }
}

我用来测试的xml

<?xml version="1.0"?>
<CIM CIMVERSION="2.0" DTDVERSION="2.0">
  <MESSAGE ID="4711" PROTOCOLVERSION="1.0">
    <SIMPLEREQ>
      <VALUE.NAMEDINSTANCE xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <INSTANCENAME CLASSNAME="DCIM_ControllerView">
          <KEYBINDING NAME="InstanceID">
            <KEYVALUE VALUETYPE="string">RAID.Slot.1-1</KEYVALUE>
          </KEYBINDING>
        </INSTANCENAME>
        <INSTANCE CLASSNAME="DCIM_ControllerView">
          <PROPERTY NAME="DriverVersion" TYPE="string">
            <DisplayValue/>
          </PROPERTY>
          <PROPERTY NAME="KeyID" TYPE="string">
            <DisplayValue/>
          </PROPERTY>
          <PROPERTY NAME="SASAddress" TYPE="string">
            <VALUE>5782BCB00C577600</VALUE>
            <DisplayValue>5782BCB00C577600</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="ProductName" TYPE="string">
            <VALUE>PERC H310 Adapter</VALUE>
            <DisplayValue>
              PERC
              H310 Adapter
            </DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="DeviceCardSlotType" TYPE="string">
            <VALUE>PCI Express x8</VALUE>
            <DisplayValue>
              PCI Express
              x8
            </DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="DeviceCardManufacturer" TYPE="string">
            <VALUE>DELL</VALUE>
            <DisplayValue>DELL</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCISubDeviceID" TYPE="string">
            <VALUE>1F4E</VALUE>
            <DisplayValue>1F4E</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCIDeviceID" TYPE="string">
            <VALUE>73</VALUE>
            <DisplayValue>73</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCISubVendorID" TYPE="string">
            <VALUE>1028</VALUE>
            <DisplayValue>1028</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCIVendorID" TYPE="string">
            <VALUE>1000</VALUE>
            <DisplayValue>1000</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="Function" TYPE="string">
            <VALUE>0</VALUE>
            <DisplayValue>0</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="Device" TYPE="string">
            <VALUE>0</VALUE>
            <DisplayValue>0</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="Bus" TYPE="string">
            <VALUE>1</VALUE>
            <DisplayValue>1</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="ControllerFirmwareVersion" TYPE="string">
            <VALUE>20.10.1-0066</VALUE>
            <DisplayValue>
              20.10.1-
              0066
            </DisplayValue>
          </PROPERTY>
        </INSTANCE>
      </VALUE.NAMEDINSTANCE>
      <VALUE.NAMEDINSTANCE xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <INSTANCENAME CLASSNAME="DCIM_ControllerView">
          <KEYBINDING NAME="InstanceID">
            <KEYVALUE VALUETYPE="string">RAID.Slot.1-1</KEYVALUE>
          </KEYBINDING>
        </INSTANCENAME>
        <INSTANCE CLASSNAME="DCIM_ControllerView">
          <PROPERTY NAME="DriverVersion" TYPE="string">
            <DisplayValue/>
          </PROPERTY>
          <PROPERTY NAME="KeyID" TYPE="string">
            <DisplayValue/>
          </PROPERTY>
          <PROPERTY NAME="SASAddress" TYPE="string">
            <VALUE>5782BCB00C577600</VALUE>
            <DisplayValue>5782BCB00C577600</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="ProductName" TYPE="string">
            <VALUE>PERC H310 Adapter</VALUE>
            <DisplayValue>
              PERC
              H310 Adapter
            </DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="DeviceCardSlotType" TYPE="string">
            <VALUE>PCI Express x8</VALUE>
            <DisplayValue>
              PCI Express
              x8
            </DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="DeviceCardManufacturer" TYPE="string">
            <VALUE>DELL</VALUE>
            <DisplayValue>DELL</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCISubDeviceID" TYPE="string">
            <VALUE>1F4E</VALUE>
            <DisplayValue>1F4E</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCIDeviceID" TYPE="string">
            <VALUE>73</VALUE>
            <DisplayValue>73</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCISubVendorID" TYPE="string">
            <VALUE>1028</VALUE>
            <DisplayValue>1028</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="PCIVendorID" TYPE="string">
            <VALUE>1000</VALUE>
            <DisplayValue>1000</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="Function" TYPE="string">
            <VALUE>0</VALUE>
            <DisplayValue>0</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="Device" TYPE="string">
            <VALUE>0</VALUE>
            <DisplayValue>0</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="Bus" TYPE="string">
            <VALUE>1</VALUE>
            <DisplayValue>1</DisplayValue>
          </PROPERTY>
          <PROPERTY NAME="ControllerFirmwareVersion" TYPE="string">
            <VALUE>20.10.1-0066</VALUE>
            <DisplayValue>
              20.10.1-
              0066
            </DisplayValue>
          </PROPERTY>
        </INSTANCE>
      </VALUE.NAMEDINSTANCE>

    </SIMPLEREQ>
  </MESSAGE>
</CIM>