XmlReader 如何读取或跳过并不总是存在的特定子项
XmlReader how to read or skip a specific child that does not always exist
我有一个很大的 XML 文件,我必须使用 XmlReader 读取它,因为它无法加载到内存中。这个XML是这样格式化的(是缩小版):
<?xml version="1.0" encoding="windows-1252"?>
<Products>
<Product>
<Code>A14</Code>
<Name>Name1</Name>
<Manufacturer>
<Name>ManufacturerName</Name>
</Manufacturer>
<ProdCategories>
<ProdCategory>
<Code>015</Code>
<Name>ProdCategoryName</Name>
</ProdCategory>
</ProdCategories>
<Barcodes> <!-- note this line -->
</Barcodes>
</Product>
<Product>
<Code>A15</Code>
<Name>Name2</Name>
<Manufacturer>
<Name>ManufacturerName</Name>
</Manufacturer>
<ProdCategories>
<ProdCategory>
<Code>016</Code>
<Name>ProdCategoryName</Name>
</ProdCategory>
</ProdCategories>
<Barcodes>
<Barcode>
<Code>1234567890</Code> <!-- note this line -->
</Brcode>
</Barcodes>
</Product>
注意 <Barcode>
<Code>
元素:缺少第一个 <product>
。
这是我用来读取它并将这些数据放入数据库的代码:
XmlReader reader = XmlReader.Create("Products.xml");
reader.MoveToContent();
do
{
reader.ReadToFollowing("Code");
code = reader.ReadElementContentAsString();
reader.ReadToFollowing("Name");
Name = reader.ReadElementContentAsString();
reader.ReadToFollowing("Name");
ManufacturerName = reader.ReadElementContentAsString();
reader.ReadToFollowing("Code");
ProdCategoryCode = reader.ReadElementContentAsString();
reader.ReadToFollowing("Code");
BarcodeCode = reader.ReadElementContentAsString();
//Here I use "code", "Name", "ManufacturerName" variables to insert into a database
} while (reader.Read());
reader.Close();
所有 XML 标签都存在于所有产品中,除了仅存在于某些产品上的 <Barcodes>
子项 (<Barcode><Code>
),那么我无法跳转到下一个 "code" 最后一个 ReadToFollowing
因为如果不存在我会捕获第一个 <product><code>
.
我无法控制 XML 输出并且无法修改它(是第三方)。
有办法“ReadToFollowing('<Barcodes><Barcode><Code>')
”让我可以具体找什么,没找到可以跳转?
感谢您的帮助,请原谅我的英语不好。
我建议将每个 Product
元素拉入树模型,使用 https://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom(v=vs.110).aspx or https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.readnode(v=vs.110).aspx,然后您可以使用 LINQ to XML 查询方法或 XPath 来读取数据每个Product
以安全的方式,同时保持低内存占用。
我有一个很大的 XML 文件,我必须使用 XmlReader 读取它,因为它无法加载到内存中。这个XML是这样格式化的(是缩小版):
<?xml version="1.0" encoding="windows-1252"?>
<Products>
<Product>
<Code>A14</Code>
<Name>Name1</Name>
<Manufacturer>
<Name>ManufacturerName</Name>
</Manufacturer>
<ProdCategories>
<ProdCategory>
<Code>015</Code>
<Name>ProdCategoryName</Name>
</ProdCategory>
</ProdCategories>
<Barcodes> <!-- note this line -->
</Barcodes>
</Product>
<Product>
<Code>A15</Code>
<Name>Name2</Name>
<Manufacturer>
<Name>ManufacturerName</Name>
</Manufacturer>
<ProdCategories>
<ProdCategory>
<Code>016</Code>
<Name>ProdCategoryName</Name>
</ProdCategory>
</ProdCategories>
<Barcodes>
<Barcode>
<Code>1234567890</Code> <!-- note this line -->
</Brcode>
</Barcodes>
</Product>
注意 <Barcode>
<Code>
元素:缺少第一个 <product>
。
这是我用来读取它并将这些数据放入数据库的代码:
XmlReader reader = XmlReader.Create("Products.xml");
reader.MoveToContent();
do
{
reader.ReadToFollowing("Code");
code = reader.ReadElementContentAsString();
reader.ReadToFollowing("Name");
Name = reader.ReadElementContentAsString();
reader.ReadToFollowing("Name");
ManufacturerName = reader.ReadElementContentAsString();
reader.ReadToFollowing("Code");
ProdCategoryCode = reader.ReadElementContentAsString();
reader.ReadToFollowing("Code");
BarcodeCode = reader.ReadElementContentAsString();
//Here I use "code", "Name", "ManufacturerName" variables to insert into a database
} while (reader.Read());
reader.Close();
所有 XML 标签都存在于所有产品中,除了仅存在于某些产品上的 <Barcodes>
子项 (<Barcode><Code>
),那么我无法跳转到下一个 "code" 最后一个 ReadToFollowing
因为如果不存在我会捕获第一个 <product><code>
.
我无法控制 XML 输出并且无法修改它(是第三方)。
有办法“ReadToFollowing('<Barcodes><Barcode><Code>')
”让我可以具体找什么,没找到可以跳转?
感谢您的帮助,请原谅我的英语不好。
我建议将每个 Product
元素拉入树模型,使用 https://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom(v=vs.110).aspx or https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.readnode(v=vs.110).aspx,然后您可以使用 LINQ to XML 查询方法或 XPath 来读取数据每个Product
以安全的方式,同时保持低内存占用。