• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

如何读取Xml内容到DataSet中

youzm 2004-11-02 10:22:20
Xml文件样例
<DataSet>
<2004年>
<月份>
<1月>
<1日>AAAA</1日>
<2日>BBBB</2日>
<3日>CCCC</3日>
<4日>DDDD</4日>
</1月>
<2月>
<1日>AAAA</1日>
<2日>BBBB</2日>
<3日>CCCC</3日>
<4日>DDDD</4日>
</2月>
</月份>
</2004年>
<2003年>
</2003年>
</DataSet>


现在我如何才能把每月的数据读取到DataSet中,并绑定到DataGrid中
...全文
338 点赞 收藏 20
写回复
20 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
youzm 2004-11-03
楼上的,具体如何使用, 能否直接将objNodeList赋值给 DataTable或DataSet.
回复
goody9807 2004-11-03
用这个 objNodeList = objNode.SelectNodes(“../Department)
取出下面两层 就能得到了啊
回复
criedshy 2004-11-03
还没有解决吗,,这个我不会,帮你顶
回复
youzm 2004-11-03
咯吱
回复
youzm 2004-11-02
谢谢大家的帮助.

我不知能否通过 \\DataSet\\2004年\\月份 这个路径来直接读取Xml的数据到DataSet中, 另外, 在性能上有限制.
回复
goody9807 2004-11-02
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
' Create a DataSet with one table and two columns.
Dim OriginalDataSet As New DataSet("myDataSet")
OriginalDataSet.Namespace = "NetFrameWork"
Dim myTable As New DataTable("myTable")
Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
c1.AutoIncrement = True
Dim c2 As New DataColumn("item")
myTable.Columns.Add(c1)
myTable.Columns.Add(c2)
OriginalDataSet.Tables.Add(myTable)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = myTable.NewRow()
newRow("item") = "item " + i.ToString()
myTable.Rows.Add(newRow)
Next i
OriginalDataSet.AcceptChanges()
' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(OriginalDataSet, "Original DataSet")
' Write the XML schema and data to file with FileStream.
Dim xmlFilename As String = "myXmlDocument.xml"
' Create FileStream
Dim fsWriteXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Create)
' Create an XmlTextWriter to write the file.
Dim xmlWriter As New System.Xml.XmlTextWriter _
(fsWriteXml, System.Text.Encoding.Unicode)
' Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter)
' Close the FileStream.
fsWriteXml.Close()

' Dispose of the original DataSet.
OriginalDataSet.Dispose()
' Create a new DataSet.
Dim newDataSet As New DataSet("New DataSet")

' Read the XML document back in.
' Create new FileStream to read schema with.
Dim fsReadXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
' Create an XmlTextReader to read the file.
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
' Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader)
' Close the XmlTextReader
myXmlReader.Close()

' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(newDataSet, "New DataSet")
End Sub


Private Sub PrintValues(ds As DataSet, label As String)
Console.WriteLine(ControlChars.Cr + label)
Dim t As DataTable
Dim r As DataRow
Dim c As DataColumn
For Each t In ds.Tables
Console.WriteLine("TableName: " + t.TableName)
For Each r In t.Rows
For Each c In t.Columns
Console.Write(ControlChars.Tab + " " + r(c).ToString())
Next c
Console.WriteLine()
Next r
Next t
End Sub
回复
godblessyU 2004-11-02
用IE打开XML文件,看看能不能显示,以判断XML串是否正确~
回复
zhwb36 2004-11-02
我想可能是你的XML文件中的数据并不是规范的数据记录,2003节点下就没有子节点,程序读的时候就不知道到底有哪些字段。
回复
yichuan1982 2004-11-02
直接读取是不会出错的

1:我用过很多次没有出错
2:如果不能直接读取,那么这个方法设计出来就没有意义

所以应该是你的XML的问题
回复
BearRui 2004-11-02
试过了,但直接读取会出错
-----------------
如果直接读取报错的话,可能是你的XML文件有问题
回复
tiktak3 2004-11-02
string dataFile = Server.MapPath("test.xml");//引号内写XML文件路径
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(dataFile);

DataGrid1.DataSource = ds;
DataGrid1.DataBind();
回复
gshope 2004-11-02
private void DemonstrateReadWriteXMLDocumentWithStreamReader(){
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable myTable = new DataTable("myTable");
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement= true;
DataColumn c2 = new DataColumn("item");
myTable.Columns.Add(c1);
myTable.Columns.Add(c2);
OriginalDataSet.Tables.Add(myTable);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++){
newRow = myTable.NewRow();
newRow["item"]= "item " + i;
myTable.Rows.Add(newRow);
}
OriginalDataSet.AcceptChanges();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(OriginalDataSet, "Original DataSet");
// Write the schema and data to an XML file.
string xmlFilename = "myXmlDocument.xml";
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlFilename);
// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document into the DataSet.
newDataSet.ReadXml(xmlFilename);
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,"New DataSet");
}

private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
回复
youzm 2004-11-02
试过了,但直接读取会出错
回复
gshope 2004-11-02
DataSet.ReadXml()
回复
youzm 2004-11-02
路过了就出来吱一声吧
回复
youzm 2004-11-02
好像各位还没解决我的问题: 因为我这份Xml文档共有五层结构, 目前的问题所在就是我只想要把最下面两层数据显示在表格里,
回复
goody9807 2004-11-02
2.2 查询已知相对路径的节点(集)
可使用类似于文件路径的相对路径的方式来查询XML的数据
objNode = objDoc.SelectSingleNode(“Company/Department”)
objNodeList = objNode.SelectNodes(“../Department)
objNode = objNode.SelectNode(“Employees/Employee”)
2.3 查询已知元素名的节点(集)
在使用不规则的层次文档时,由于不知道中间层次的元素名,可使用//符号来越过中间的节点,查询其子,孙或多层次下的其他所有元素。例如:
objNodeList = objDoc.SelectNodes(“Company//Employee”)
2.4 查询属性(attribute)节点
以上的各种方法都返回元素(element)节点(集),返回属性(attribute),只需要采用相应的方法,在属性名前加一个@符号即可,例如:
objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee/@id”)
objNodeList = objDoc.SelectNodes(“Company//@id”)
2.5 查询Text节点
使用text()来获取Text节点。
objNode = objDoc.SelectSingleNode(“Company/Department/Deparmt_Name/text()”)
2.6 查询特定条件的节点
使用[]符号来查询特定条件的节点。例如:
a. 返回id号为 10102的Employee节点
objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee[@id=’10102’]”)
b. 返回Name为Zhang Qi的Name 节点
objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee/Name[text()=’Zhang Qi’]”)
c. 返回部门含有职员22345的部门名称节点
objNode = objDoc.SelectSingleNode("Company/Department[Employees/Employee/@id='22345']/Department_Name")
2.7 查询多重模式的节点
使用 | 符号可以获得多重模式的节点。例如:
objNodeList = objDoc.SelectNodes(“Company/Department/Department_Name | Company/Department/Manager”)
2.8 查询任意子节点
使用*符号可以返回当前节点的所有子节点。
objNodeList = objDoc.SelectNodes(“Company/*/Manager)
或者
objNodeList = objNode.ChildNodes
回复
和楼上的一样了,声明:该代码不是我写的,我也是从另一篇帖子里复制的.
回复
private void DemonstrateReadWriteXMLDocumentWithXMLReader(){
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable myTable = new DataTable("myTable");
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement= true;
DataColumn c2 = new DataColumn("item");
myTable.Columns.Add(c1);
myTable.Columns.Add(c2);
OriginalDataSet.Tables.Add(myTable);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++){
newRow = myTable.NewRow();
newRow["item"]= "item " + i;
myTable.Rows.Add(newRow);
}
OriginalDataSet.AcceptChanges();
// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(OriginalDataSet, "Original DataSet");
// Write the XML schema and data to file with FileStream.
string xmlFilename = "myXmlDocument.xml";
// Create FileStream
System.IO.FileStream fsWriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Create an XmlTextWriter to write the file.
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(fsWriteXml, System.Text.Encoding.Unicode);
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter);
// Close the FileStream.
fsWriteXml.Close();

// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");

// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader.Close();

// Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,"New DataSet");
}

private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
回复
用DATASET直接读取的XML文档不能超过三层,你超过三层了.
回复
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-11-02 10:22
社区公告
暂无公告