0

我通过在 c#.net 中导出我的 SQLite 数据库生成了一个 XML 文件。我生成的 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 数据,例如 -

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

我已将此 XML 文件导入 SQLite 数据库。我使用以下代码导入 -

SQLiteConnection 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);
            string xsdFilename = "myXSD.xsd";
            string xmlFilename = "myXML.xml";

            sqliteDatabase.ReadXmlSchema(xsdFilename);
            sqliteDatabase.ReadXml(xmlFilename);

sqliteDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity);

现在,问题是它适用于普通 XML 文件,我无法导入从普通 XML 转换的平面 XML 文件。谁能帮我修改它,以便我也可以从平面 XML 导入数据?

4

1 回答 1

0

需要考虑的一个问题是,SQLite 是否能够识别 XML 并将其与生成完整 XML 的数据库模式相匹配,即使对展平的 XML 有正确的 XSD。为什么首先需要展平 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>
于 2014-04-15T14:07:17.263 回答