【问题标题】:Having trouble importing a serialized XML into SQL table将序列化的 XML 导入 SQL 表时遇到问题
【发布时间】:2019-03-11 09:42:56
【问题描述】:

我有一个应用程序,它为每个填写的 .net 表单创建以下 XML 文档。我只需要将 xml 的数据集(报告)部分中的值字段提取到 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>

【问题讨论】:

  • 你试过什么?,你在这里问之前有没有搜索过?

标签: c# sql .net xml serialization


【解决方案1】:

如果您知道您的登录信息是关于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的模型。

第二种方法:

此代码可能对来自XMl 代码的Read 数据有帮助:

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

【讨论】:

    【解决方案2】:

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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                DataSet ds = new DataSet();
                ds.ReadXml(FILENAME);
            }
        }
    }
    

    【讨论】:

    • Declare @x xml select @x=P from openrowset (bulk '\\server\sm5jlcnn.xml', single_blob) as Products(P) select @x as txml into #ttt
    • 对不起,我很痛苦,但我没有使用 c# 或 xml 的经验,只有 SQL,我创建了一个存储过程来将 xml 文件作为单个博客类型批量插入:xml,我确实安装了 studio &让控制台 xml 读取代码正常工作,但是,我不知道如何将其写入 sql 表。我可以使用 xQuery 查询存储在单个字段中的 xml 代码吗?还是需要先反序列化?上面的 sql 代码将整个 xml 文件放入单个 sql 表字段中。然后我打算为我需要的每个字段编写选择/插入语句,这有意义吗?还是文件需要第一次准备
    • 如果您在 c# 数据集中有代码,您可以使用 c# 中的 SQLClient 库将数据集批量复制到数据库。见 msdn:docs.microsoft.com/en-us/dotnet/api/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    相关资源
    最近更新 更多