C#和XML文件解析 [英] C# and XML file parsing

查看:134
本文介绍了C#和XML文件解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我希望有人可以帮助我.
我正在尝试解析一个非常大的XML文件.我首先逐行读取文件,然后使用XML阅读器解析每一行.

这行看起来像这样(文件有大约20000行是这样的):

Hi guys,
I am hoping somebody can help me with this.
I am trying to parse a very large XML file. I am first reading the file line by line and then using XML reader to parse each line.

The line looks like this (The file has about 20000 lines like this):

<fieldinfo><fieldno>ABCDE</fieldno><desc><![CDATA[bubbles</desc><indicative><comcd>FCH</comcd><prodcls>6Q</prodcls><actyind>M</actyind><mi>003</mi><assmind>0</assmind><attind>0</attind><noncurrdt>0401</noncurrdt><frtrdind>0</frtrdind><fldcarrind>0</fldcarrind><hazmattind>0</hazmattind><pkgqtyind>0</pkgqtyind><pkgqty>1</pkgqty><weight><net>0.51</net><gross>0.51</gross></weight><dimension><len>4.5</len><ht>1.7</ht><wdt>4.0</wdt></dimension><rplmts><ind>0</ind><type>0</type></rplmts></indicative><pricing><nmp>NO</nmp><dlrcd>AB31</dlrcd><mkttyp>PRODUCT</mkttyp><aggtyp>TEPSF</aggtyp><mo>NACD</mo><sa>NACD  </sa><ccy>USD</ccy><effdt>20110701</effdt><net><prdprc>32.31</prdprc><crdep>1.0</crdep><dmgcrrfnd>0.0</dmgcrrfnd><totprcwdmgcr>0.0</totprcwdmgcr></net><lst><prdprc>46.0</prdprc><crdep>0.0</crdep><dmgcrrfnd>0.0</dmgcrrfnd><totprcwdmgcr>0.0</totprcwdmgcr></lst><fon><prdprc>41.4</prdprc><crdep>0.0</crdep><dmgcrrfnd>0.0</dmgcrrfnd><totprcwdmgcr>0.0</totprcwdmgcr></fon><otr><prdprc>33.96</prdprc><crdep>0.0</crdep><dmgcrrfnd>0.0</dmgcrrfnd><totprcwdmgcr>0.0</totprcwdmgcr></otr><stk><prdprc>0.0</prdprc><crdep>0.0</crdep><dmgcrrfnd>0.0</dmgcrrfnd><totprcwdmgcr>0.0</totprcwdmgcr></stk></pricing></fieldinfo>



我正在尝试从文件行中解析各种元素值,然后读取另一行.
我需要解析的字段是:
< fieldno>
< desc>
< net>< prdprc>
< lst>< prdprc>值等

我目前正在使用来自Microsoft网站的示例,该案例使用case语句读取元素的名称,然后使用ReadToFollowing转到诸如prdPrc之类的字段.由于某些原因,某些值未返回,而其他则返回.请帮忙!!!

在此先感谢



I am trying to parse various element values from the file lines and then reading another line.
Fields I need to parse are:
<fieldno>
<desc>
<net><prdprc>
<lst><prdprc> value and so on

I am currently using an example from microsoft website using a case statement to read the name of the element and then using ReadToFollowing to go to fields like prdPrc. For some reason some values are not being returned and others are. Please help!!!

Thanks in advance

推荐答案

好,不要读取文件.使用.NET XML解析器之一.以下是它们的简短概述:

Well, don''t read file. Use one of .NET XML parsers. Here is short overview of them:


  1. 使用System.Xml.XmlDocument类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
    请参见
  2. 使用类System.Xml.XmlTextReader; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ ].
  3. 使用类System.Xml.XmlTextReader;这是最快的读取方法,尤其是您需要跳过一些数据.
    请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].



由于文件很大,因此System.Xml.XmlTextReader可能是最好的.但是,这还取决于您要对解析结果执行什么操作.

—SA



As your file is big, System.Xml.XmlTextReader might be the best. However, it also depends on what do you want to do with the results of parsing.

—SA


您使用以下C#代码解析xml,u将获得每个元素名称和文本
包含那个.

添加名称空间-使用System.Xml;
在按钮上单击,将代码写为-
XmlTextReader reader = new XmlTextReader("C:\\ Users \\ Dell \\ Desktop \\ New folder \\ XMLFile1.xml"); //这将是xml的位置和名称

while(reader.Read())
{
开关(reader.NodeType)
{
case XmlNodeType.Element://节点是一个元素.
MessageBox.Show(<" + reader.Name);
//MessageBox.Show(">);
//MessageBox.Show(reader.Value);
休息;
case XmlNodeType.Text://显示每个元素中的文本.
MessageBox.Show(reader.Value);
休息;
case XmlNodeType.EndElement://显示元素的结尾.
//Console.Write("</"+ reader.Name);
//MessageBox.Show(">);
休息;
}
}

通过此示例,您可以读取任何类型的xml并检索任何节点,内部节点或xml的任何文本.
You Parse your xml using following C# code,u will get each element name and text
containg that.

add namespace - using System.Xml;
on button click write code as-
XmlTextReader reader = new XmlTextReader("C:\\Users\\Dell\\Desktop\\New folder\\XMLFile1.xml"); //this will location and name of xml

while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
MessageBox.Show("<" + reader.Name);
//MessageBox.Show(">");
//MessageBox.Show(reader.Value);
break;
case XmlNodeType.Text: //Display the text in each element.
MessageBox.Show(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("</" + reader.Name);
//MessageBox.Show(">");
break;
}
}

By this sample u can read any type of xml and retrive any node,innernode or any text of xml.


这篇关于C#和XML文件解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆