挣扎于 xml - 想要将各种类型的文件中的任何一种读入数据集
Struggling with xml - want to read any of various types of file into a dataset
我需要读取各种类型的 xml 文件并解析数据。似乎将文件读入数据集是可行的方法,但我很难这样做。
我想知道我的测试文件是不是格式不正确,但我下载了一个示例文件和模式,它也失败了。根据此测试代码,所有文件都可以正常加载到文档中:
Dim ds As New DataSet()
For thisFile As Integer = 0 To readFiles.Count - 1
frmMain.UpdateInfo(readFiles.Item(thisFile), "I") ' tell the user
Dim doc As New Xml.XmlDocument
doc.Load(readFiles.Item(thisFile)) ' this works
Dim thisFileReader As System.IO.StringReader = New System.IO.StringReader(readFiles.Item(thisFile))
ds.Clear()
ds.ReadXml(thisFileReader, XmlReadMode.IgnoreSchema) ' this fails with "Data at the root level is invalid. Line 1, position 1."
For Each thisTable As DataTable In ds.Tables
' action...
Next
Next
示例文件如下所示:
<?xml version="1.0" encoding="UTF-8"?><shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"><orderperson>John Smith</orderperson><shipto><name>Ola Nordmann</name><address>Langgt 23</address><city>4000 Stavanger</city><country>Norway</country></shipto><item><title>Empire Burlesque</title><note>Special Edition</note><quantity>1</quantity><price>10.90</price></item><item><title>Hide your heart</title><quantity>1</quantity><price>9.90</price></item></shiporder>
(请注意,我有意删除了格式,因为我将要阅读的文件很相似。)
示例架构如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
两者都在同一个文件夹中进行测试,但我认为当我将读取模式设置为 IgnoreSchema 时不应使用架构。
我确定我错过了一些非常简单的事情...
尝试使用 StreamReader
而不是 StringReader
,并尝试不使用 XmlReadMode.IgnoreSchema
。
Dim thisFileReader As System.IO.StreamReader = New System.IO.StringReader(readFiles.Item(thisFile))
ds.Clear()
ds.ReadXml(thisFileReader)
我需要读取各种类型的 xml 文件并解析数据。似乎将文件读入数据集是可行的方法,但我很难这样做。
我想知道我的测试文件是不是格式不正确,但我下载了一个示例文件和模式,它也失败了。根据此测试代码,所有文件都可以正常加载到文档中:
Dim ds As New DataSet()
For thisFile As Integer = 0 To readFiles.Count - 1
frmMain.UpdateInfo(readFiles.Item(thisFile), "I") ' tell the user
Dim doc As New Xml.XmlDocument
doc.Load(readFiles.Item(thisFile)) ' this works
Dim thisFileReader As System.IO.StringReader = New System.IO.StringReader(readFiles.Item(thisFile))
ds.Clear()
ds.ReadXml(thisFileReader, XmlReadMode.IgnoreSchema) ' this fails with "Data at the root level is invalid. Line 1, position 1."
For Each thisTable As DataTable In ds.Tables
' action...
Next
Next
示例文件如下所示:
<?xml version="1.0" encoding="UTF-8"?><shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"><orderperson>John Smith</orderperson><shipto><name>Ola Nordmann</name><address>Langgt 23</address><city>4000 Stavanger</city><country>Norway</country></shipto><item><title>Empire Burlesque</title><note>Special Edition</note><quantity>1</quantity><price>10.90</price></item><item><title>Hide your heart</title><quantity>1</quantity><price>9.90</price></item></shiporder>
(请注意,我有意删除了格式,因为我将要阅读的文件很相似。)
示例架构如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
两者都在同一个文件夹中进行测试,但我认为当我将读取模式设置为 IgnoreSchema 时不应使用架构。
我确定我错过了一些非常简单的事情...
尝试使用 StreamReader
而不是 StringReader
,并尝试不使用 XmlReadMode.IgnoreSchema
。
Dim thisFileReader As System.IO.StreamReader = New System.IO.StringReader(readFiles.Item(thisFile))
ds.Clear()
ds.ReadXml(thisFileReader)