紧急求救,怎么样才能读取这个xml文件的数据 其实如果楼主要顺序读,可以用XMLTEXTREADER进行读取,如果不是,则需要通过XMLDOC的方式读取,相关的帮助中有很好的例程,可以去看看,然后仿制一个就OK了. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 xmltextreader 中的例子都太简单了,没有嵌套3层的,唉。恳请高手帮忙啊,不甚感激!!! 做一个 xsd ,确定好内表的关系,然后用 dataset 来读,无非就是嵌套子表 here are two examples, as you can see, it might be easier for you to use XmlDocumentusing 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()); } }} 多谢各位大侠,特别是saucer(思归/MVP) 。另,anggogo(angGoGo) ,其实我是有这个xsd的,并且也生成了 strong dataset,但是就是不明如何把这个xml导入到数据库呢? 谢谢,愿闻其详晚上揭帖。 vs 2010 发布的程序不能在其它机子上运行 C# 谈谈你对多层架构的认识 LINQ2SQL基础问题 winfrom程序运行期间无响应,求解决办法 Remoting技术问题 高分求救: .net中的程序能不能直接生成VISIO文档? 如何在C#中提取本机的MAC地址?急急急急急急急急!!在现等!!!!!!!!!! 初级问题2个? 高手介绍windows DNA,Com+ , MTS , 设计模式 它们之间的关系 和异同 及应用场合 .Net中 使用StreamWriter 遇到的文件编码问题 在C#中调用VC写的DLL,出现错误。 c#问题
恳请高手帮忙啊,不甚感激!!!
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());
}
}
}