将序列化的XML导入SQL表时遇到问题

时间:2018-10-05 20:51:13

标签: c# sql .net xml serialization

我有一个应用程序,可以为填写的每个.net表单创建以下XML文档。我只需要将xml的数据集(报告)部分中的value字段提取到SQL表中以进行数据仓库。我很抱歉,但是我对XML,diffgram或序列化的经验很少。我有很好的SQL技能...任何建议将不胜感激。谢谢

<?xml version="1.0" encoding="utf-8" ?>
<DataSet>
  <xs:schema id="Report" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Report" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Field">
            <xs:complexType>
              <xs:attribute name="FieldID" type="xs:string" />
              <xs:attribute name="Value" type="xs:string" />
              <xs:attribute name="MUID" type="xs:string" />
              <xs:attribute name="ParentMUID" type="xs:string" />
              <xs:attribute name="DisplayValue" type="xs:string" />
              <xs:attribute name="Ordinal" type="xs:integer" />
            </xs:complexType>
          </xs:element>
          <xs:element name="ReportInfo">
            <xs:complexType>
              <xs:attribute name="ReportID" type="xs:string" />
              <xs:attribute name="ReportType" type="xs:string" />
              <xs:attribute name="ReportNumber" type="xs:string" />
              <xs:attribute name="Date" type="xs:string" />
              <xs:attribute name="ReportPlace" type="xs:string" />
              <xs:attribute name="EmployID" type="xs:string" />
              <xs:attribute name="Status" type="xs:string" />
              <xs:attribute name="SupervisorID" type="xs:string" />
              <xs:attribute name="Info" type="xs:string" />
              <xs:attribute name="Notes" type="xs:string" />
              <xs:attribute name="CheckedOut" type="xs:string" />
              <xs:attribute name="CheckedOutTo" type="xs:string" />
              <xs:attribute name="CheckedOutBy" type="xs:string" />
            </xs:complexType>
          </xs:element>
          <xs:element name="MobileDocControlField">
            <xs:complexType>
              <xs:attribute name="FieldID" type="xs:string" />
            </xs:complexType>
          </xs:element>
          <xs:element name="MobileUserStampControlField">
            <xs:complexType>
              <xs:attribute name="FieldID" type="xs:string" />
            </xs:complexType>
          </xs:element>
          <xs:element name="SupplementExtension">
            <xs:complexType>
              <xs:attribute name="Extension" type="xs:string" />
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <Report>
      <Field diffgr:id="Field1" msdata:rowOrder="0" FieldID="39b20de0-bc9a-4e75-9dc2-dc38ec3066ac" Value="2020-225588" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field2" msdata:rowOrder="1" FieldID="27f9166c-5dd3-4ffe-97e4-bce38c393dc6" Value="10/4/2018" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field3" msdata:rowOrder="2" FieldID="807b7ec2-788c-4146-845d-61acafff71f7" Value="True" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field4" msdata:rowOrder="3" FieldID="476aedcd-6274-4883-9a20-71a0dca262e4" Value="False" MUID="" ParentMUID="" Ordinal="-1" />
      <Field diffgr:id="Field5" msdata:rowOrder="4" FieldID="e76ae04e-951a-43c7-87da-481d2e14f8f0" Value="False" MUID="" ParentMUID="" Ordinal="-1" />
      <Field diffgr:id="Field6" msdata:rowOrder="5" FieldID="6757ba62-0b28-48ab-a493-1363e553cf03" Value="False" MUID="" ParentMUID="" Ordinal="-1" />
      <Field diffgr:id="Field7" msdata:rowOrder="6" FieldID="b35a19d5-a370-477e-ae8f-54a5e5101b55" Value="False" MUID="" ParentMUID="" Ordinal="-1" />
      <Field diffgr:id="Field8" msdata:rowOrder="7" FieldID="cc39819a-bcd4-4b23-987f-d0863abaf1b6" Value="False" MUID="" ParentMUID="" Ordinal="-1" />
      <Field diffgr:id="Field9" msdata:rowOrder="8" FieldID="06b3a827-d7f2-4ac9-9c1e-a02b8cb5aadc" Value="-14209|Color" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field10" msdata:rowOrder="9" FieldID="b300d2f5-62e8-44f9-a02e-6d79f11a22c5" Value="{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}&#xD;&#xA;\viewkind4\uc1\pard\fs20 This is the open section of the document\par&#xD;&#xA;}&#xD;&#xA;" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field11" msdata:rowOrder="10" FieldID="fb01a79a-7aec-434c-9be7-b935d112d7c9" Value="10/04/2018" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field12" msdata:rowOrder="11" FieldID="cbd01999-2b3a-4458-9e65-8ca34db901a6" Value="-14222|Yes No" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field13" msdata:rowOrder="12" FieldID="06a2f8c6-1e4a-494a-b9ae-9e62d05974f7" Value="-14222|Yes No" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field14" msdata:rowOrder="13" FieldID="b8628ae9-91d4-49af-beee-02e050bf884c" Value="|Yes No" MUID="" ParentMUID="" Ordinal="-1" />
      <Field diffgr:id="Field15" msdata:rowOrder="14" FieldID="efd43c70-45a7-4c4e-bfb0-988fac69df2a" Value="-76329|-54403|YearsOfExp" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field16" msdata:rowOrder="15" FieldID="1f225b35-511b-41e8-8d3c-7209057684c9" Value="|Yes No" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field17" msdata:rowOrder="16" FieldID="41569f6a-fea0-4e99-af9c-b7ace7ef1fe1" Value="OHCLP0000|-14463" MUID="" ParentMUID="" DisplayValue="" Ordinal="-1" />
      <Field diffgr:id="Field18" msdata:rowOrder="17" FieldID="76d2b820-0ab7-490b-9db4-d97870274141" Value="Home Address" MUID="" ParentMUID="" Ordinal="-1" />
    </Report>
  </diffgr:diffgram>
</DataSet>

2 个答案:

答案 0 :(得分:0)

如果您了解有关XML的登录信息,则可以创建一个模型并Deserialize,并获取数据以插入Database

建议:

  1. 创建用于反序列化的模型。
  2. 直接从XML中获取数据。

第一种方法:

想象一下这是您的模特:

[Serializable()]
public class Car
{
    [System.Xml.Serialization.XmlElementAttribute("StockNumber")]
    public string StockNumber{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("Make")]
    public string Make{ get; set; }

    [System.Xml.Serialization.XmlElementAttribute("Model")]
    public string Model{ get; set; }
}

并使用以下代码对XML进行反序列化:

CarCollection cars = null;
string path = "cars.xml";

XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));

StreamReader reader = new StreamReader(path);
cars = (CarCollection)serializer.Deserialize(reader);
reader.Close();

现在您有了模型,数据为XML

第二种方法:

此代码可能有助于处理Read代码中的XMl数据:

public List<DataAttr> GetDataAttribute(XDocument xDocument)
    {
        var dataAttr = new List<DataAttr>();
        foreach (var route in xDocument.Descendants("RoutePoints"))
        {
            foreach (var point in route.Elements())
            {
                if (point.HasElements)
                {
                    dataAttr.AddRange(_getAttributeValues(point.Attributes()));
                }
            }
        }
        return dataAttr;
    }

    private static IEnumerable<DataAttr> _getAttributeValues(IEnumerable<XAttribute> attributes)
    {
        return attributes.Select(x => new DataAttr()
        {
            Name = x.Name.LocalName,
            Value = x.Value
        });
    }

使用此代码,您可以从XML读取数据并选择NodesElementsAttributes

答案 1 :(得分:0)

使用以下代码非常简单地读取xml:

yarn --dev add @babel/core babel-loader @babel/preset-env @babel/preset-react