紧急求救,怎么样才能读取这个xml文件的数据

daviddot 2004-07-26 12:20:36
xml格式大致如下:

<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<tab_1>
<Vendor_ID>3</Vendor_ID>
<PlayBeginDate>2004-6-21 0:00:00</PlayBeginDate>
<tab_2>
<CopyKey/>
<Program_cn>生活剧场片头</Program_cn>
<tab_3>
<PlayDate>2004-6-21 0:00:00</PlayDate>
<NoOfSpots>0</NoOfSpots>
</tab_3>
<tab_3>
<PlayDate>2004-3-2 0:00:00</PlayDate>
<NoOfSpots>1</NoOfSpots>
</tab_3>
</tab_2>
<tab_2>
<CopyKey>56</CopyKey>
<Program_cn>饮食剧场片头</Program_cn>
<tab_3>
<PlayDate>2004-6-22 0:00:00</PlayDate>
<NoOfSpots>1</NoOfSpots>
</tab_3>
<tab_3>
<PlayDate>2004-7-8 0:00:00</PlayDate>
<NoOfSpots>0</NoOfSpots>
</tab_3>
</tab_2>
</tab_1>

简单的说,就是 <tab_1> 里包含了若干个<tab_2>,
而<tab_2>里又包含了若干个<tab_3>.
想用 xmlTextReader 或者是 xmlNodeReader 来读取数据,苦于水平有限,一直没有成功。
请教大虾, 极度感谢!!!!!!!!!!
...全文
152 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyincs 2004-07-26
  • 打赏
  • 举报
回复
mark
saucer 2004-07-26
  • 打赏
  • 举报
回复
here are two examples, as you can see, it might be easier for you to use XmlDocument


using System;
using System.Xml;

class TestNested
{
public static void Main()
{
string sFile = "TestNested.xml";
Console.WriteLine("****DOM Method***");
Test1(sFile);
Console.WriteLine("****XmlTextReader method***");
Test2(sFile);
}

static void Test1(string sFile)
{

XmlDocument doc = new XmlDocument();
doc.Load(sFile);

XmlElement e = (XmlElement)doc.SelectSingleNode("tab_1");
//Console.WriteLine(e.Name);
foreach (XmlElement ex in e.SelectNodes("tab_2"))
{
Console.WriteLine(ex["CopyKey"].InnerText);
foreach (XmlElement ex2 in ex.SelectNodes("tab_3"))
{
Console.WriteLine(ex2["PlayDate"].InnerText);
}
}
}


static void Test2(string sFile)
{
XmlTextReader xtr = new XmlTextReader(sFile);
while (xtr.Read())
{
if (xtr.NodeType == XmlNodeType.Element && xtr.LocalName == "tab_2")
ReadTab2(xtr);
}

xtr.Close();
}

static void ReadTab2(XmlTextReader xtr)
{
while (xtr.Read())
{
if (xtr.NodeType == XmlNodeType.EndElement && xtr.LocalName == "tab_2")
break;

if (xtr.NodeType == XmlNodeType.Element && xtr.LocalName == "CopyKey")
Console.WriteLine(xtr.ReadString());

if (xtr.NodeType == XmlNodeType.Element && xtr.LocalName == "tab_3")
ReadTab3(xtr);


}
}


static void ReadTab3(XmlTextReader xtr)
{
while (xtr.Read())
{
if (xtr.NodeType == XmlNodeType.EndElement && xtr.LocalName == "tab_3")
break;

if (xtr.NodeType == XmlNodeType.Element && xtr.LocalName == "PlayDate")
Console.WriteLine(xtr.ReadString());


}
}
}
daviddot 2004-07-26
  • 打赏
  • 举报
回复
多谢各位大侠,特别是saucer(思归/MVP) 。

另,anggogo(angGoGo) ,其实我是有这个xsd的,并且也生成了 strong dataset,但是就是不明如何把这个xml导入到数据库呢? 谢谢,愿闻其详

晚上揭帖。
anggogo 2004-07-26
  • 打赏
  • 举报
回复
做一个 xsd ,确定好内表的关系,然后用 dataset 来读,无非就是嵌套子表
daviddot 2004-07-26
  • 打赏
  • 举报
回复
xmltextreader 中的例子都太简单了,没有嵌套3层的,唉。
恳请高手帮忙啊,不甚感激!!!
zag 2004-07-26
  • 打赏
  • 举报
回复
其实如果楼主要顺序读,可以用XMLTEXTREADER进行读取,如果不是,则需要通过XMLDOC的方式读取,相关的帮助中有很好的例程,可以去看看,然后仿制一个就OK了.
daviddot 2004-07-26
  • 打赏
  • 举报
回复
请大家帮忙呀,立即给分。 极度极度感谢!!!!!!!!!!呵呵。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧