(在线等)c#读取xml速度问题解决方案

来源:百度知道 编辑:UC知道 时间:2024/06/01 09:21:17
我现在导出了3w条数据的xml文件
例:
<NewDataSet>
<Table>
<ProductCode>08yangyi02002</ProductCode>
<Price>1000.0000</Price>
<ProductStyle>08yangyi</ProductStyle>
<ProductColor>020</ProductColor>
<SizeName>M</SizeName>
<ProductName>样衣</ProductName>
<UnitName>件</UnitName>
</Table>
我要根据传入ProductCode的值读取一条记录,代码如下:

DataTable table = new DataTable();
table.Columns.Add(new DataColumn("ProductCode", typeof(string)));
table.Columns.Add(new DataColumn("Price", typeof(string)));
table.Columns.Add(new DataColumn("ProductStyle", typeof(string)));
table.Columns.Add(new DataColumn("ProductColor", typeof(string)));
table.Columns.Add(new DataColumn("SizeName", typ

你就是要找ProductCode为某值的所有Table?
把你的xpath改一下,一条查询就出来了
XmlNodeList tableList = doc.SelectNodes(string.Format("//ProductCode[text()='{0}']/parent::Table",TxtProductCode.Text));
然后遍历tableList取值就行了

如果数据量很大的话,用XPathDocument可以提高查询速度
====================================
"//ProductCode[text()='{0}']/parent::Table"
这条查询的意思查找任意(//)ProductCode节点,要求节点的text()返回值也就是innerText为{0},找到之后取他的父(parent)Table节点.
这样查询出来的node集合就是所有你需要的节点,不用再作判断,直接取出放进你的DataTable就行了

用这种方法试一下,这是速度最快的读取方式,数据量太大最好不要用xmldom读取,速度很慢
using System;
using System.Text;
using System.Xml;
using System.IO;
namespace HelloNamespace
{
public class A
{
static void Main(string[] args)
{

Stream s=new FileStream("..\\..\\XMLFile1.xml",FileMode.Open);
XmlReader xr = XmlReader.Create(s);