【发布时间】:2019-02-01 18:40:28
【问题描述】:
我有一个大小为 35 GB 的 XML 文件。我尝试使用 xmldocument 加载此文件并出现内存不足异常。因此,使用 xmlreader 解析 xml 数据以将其加载到数据库。但是,我无法读取父节点中的子节点。
示例 XML 文件内容:
文件名:wcproduction.xml
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
<xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd"/>
<xsd:element name="wcproduction">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="api_st_cde" type="sqltypes:smallint" nillable="1"/>
<xsd:element name="api_cnty_cde" type="sqltypes:smallint" nillable="1"/>
<xsd:element name="api_well_idn" type="sqltypes:int" nillable="1"/>
<xsd:element name="pool_idn" type="sqltypes:int" nillable="1"/>
<xsd:element name="prodn_mth" type="sqltypes:smallint" nillable="1"/>
<xsd:element name="prodn_yr" type="sqltypes:int" nillable="1"/>
<xsd:element name="ogrid_cde" type="sqltypes:int" nillable="1"/>
<xsd:element name="prd_knd_cde" nillable="1">
<xsd:simpleType>
<xsd:restriction base="sqltypes:char" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="2"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="eff_dte" type="sqltypes:datetime" nillable="1"/>
<xsd:element name="amend_ind" nillable="1">
<xsd:simpleType>
<xsd:restriction base="sqltypes:char" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="c115_wc_stat_cde" nillable="1">
<xsd:simpleType>
<xsd:restriction base="sqltypes:char" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="prod_amt" type="sqltypes:int" nillable="1"/>
<xsd:element name="prodn_day_num" type="sqltypes:smallint" nillable="1"/>
<xsd:element name="mod_dte" type="sqltypes:datetime" nillable="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<wcproduction xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
<api_st_cde>30</api_st_cde>
<api_cnty_cde>5</api_cnty_cde>
<api_well_idn>20178</api_well_idn>
<pool_idn>10540</pool_idn>
<prodn_mth>7</prodn_mth>
<prodn_yr>1973</prodn_yr>
<ogrid_cde>12437</ogrid_cde>
<prd_knd_cde>G </prd_knd_cde>
<eff_dte>1973-07-31T00:00:00</eff_dte>
<amend_ind>N</amend_ind>
<c115_wc_stat_cde>F</c115_wc_stat_cde>
<prod_amt>53612</prod_amt>
<prodn_day_num>99</prodn_day_num>
<mod_dte>2015-04-07T07:31:00.173</mod_dte>
</wcproduction>
</root>
VB.net 代码尝试读取父节点 wcproduction 及其子节点(api_st_cde, api_cnty_cde, ...)
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.IgnoreWhitespace = True
Using reader As XmlReader = XmlReader.Create("D:\\wcproduction.xml", settings)
reader.ReadToFollowing("wcproduction")
Do
Dim inner As XmlReader = reader.ReadSubtree()
Dim str As String = ""
inner.ReadToDescendant("api_st_cde")
str = inner.ReadInnerXml
inner.ReadToDescendant("api_cnty_cde")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("api_well_idn")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("pool_idn")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("prodn_mth")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("prodn_yr")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("ogrid_cde")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("prd_knd_cde")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("eff_dte")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("amend_ind")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("c115_wc_stat_cde")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("prod_amt")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("prodn_day_num")
str = str & ", " & inner.ReadInnerXml
inner.ReadToDescendant("mod_dte")
str = str & ", " & inner.ReadInnerXml
MsgBox(str)
inner.Close()
Loop While (reader.ReadToNextSibling("wcproduction"))
End Using
我想读取所有节点(wcproduction)及其子节点并将其上传到 SQL 服务器。
【问题讨论】:
-
我可以帮忙。在 c# 中多次执行此操作并愿意在 vb.net 中提供帮助。我有两个问题。 1) xml 有命名空间。最好的方法是进行序列化。此数据似乎来自 SQL 数据库。 xml 显示低级 xml 的 1 个模式位置。文件中是否还有其他架构位置? 2)如果您不使用序列化,您可以给我您尝试提取的重复元素。使用 XML 阅读器,我通常使用重复元素。我无法从发布的 xml 中看出您要提取哪些重复元素。
-
sql 数据库使用网络上可用的标准模式。 MSDN 有一个工具 xsd.exe,您可以下载它,它采用 xsd 模式并转换为 VB 类。然后,您可以使用 XML 序列化来获取您的数据库并转换为类。我可能会导致 35GB 文件出现内存错误。你能解释一下你想要做什么吗?您应该将 SQL 数据导入 SQL Server。有些命令行工具旨在处理非常大的文件。请参阅:docs.microsoft.com/en-us/sql/tools/…
-
@jdweng,重复元素是 wcproduction 及其子元素。我想从 wcproduction 中提取所有字段。我已准备好实施任何解决方案。
-
我已经编辑了答案。