在c#.net中将XML平面数据导入SQLite数据库

7uhlpewt  于 2022-12-27  发布在  SQLite
关注(0)|答案(1)|浏览(160)

我已经通过将SQLite数据库导出到c#.net中生成了一个XML文件。

<root>
  <name1>
    <names>
      <id>5</id>           
      <from>Germany</from>
      <to>France</to>
      <through>
        <via>
          <id>7</id>
          <routeNo>5<routeNo>
          <route>Vienna<route>
        </via>
      </through>           
    </names>
    <names>
      <id>10</id>           
      <from>US</from>
      <to>Canada</to>
      <through>
        <via>
          <id>8</id>
          <routeNo>10<routeNo>
          <route>Mexico<route>
        </via>
      </through>           
    </names>
  </name1>
</root>

然后我把上面的xml转换成这样一个XML文件

<names id="5" from="Germany" to="France"> 
  <through id="9" routeNo="5" route="Vienna" /> 
  <through id="10" routeNo="5" route="russia" /> 
</names>

我已经使用以下代码将此XML文件导入到SQLite数据库中:

var sqlite_conn = new SQLiteConnection(
         "Data Source=SGLight_empty.fmeda;Version=3;New=True;Compress=True;");
NDbUnit.Core.INDbUnitTest sqliteDatabase 
              = new NDbUnit.Core.SqlLite.SqlLiteUnitTest(sqlite_conn);
   
sqliteDatabase.ReadXmlSchema("myXSD.xsd");
sqliteDatabase.ReadXml("myXML.xml");
sqliteDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity);

导入适用于普通XML文件。

  • 问题是我无法导入从普通XML转换而来的平面XML文件。
  • 有谁能帮我修改一下,这样我就可以从平面XML导入数据了?
dzhpxtsq

dzhpxtsq1#

需要考虑的一个问题是,即使为扁平化的XML提供了正确的XSD,SQLite是否能够识别XML并将其与生成完整XML的数据库模式匹配。
也就是说,Windows SDK附带了一个xsd工具,可以从任意XML文件推断出XSD。我将“平面”xml复制到一个名为temp.xml的文件中,运行xsd temp.xml,并收到以下XSD定义:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="names">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="through" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="id" type="xs:string" />
            <xs:attribute name="routeNo" type="xs:string" />
            <xs:attribute name="route" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="id" type="xs:string" />
      <xs:attribute name="from" type="xs:string" />
      <xs:attribute name="to" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="names" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

相关问题