首页 新闻 会员 周边

请教一个 XML DATASET 相关的问题

1
[待解决问题]

Students.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Class ID="1" Name="MUSIC">
<Student ID="1">
<Name>LEE</Name>
<Age>16</Age>
</Student>
<Student ID="2">
<Name>WANG</Name>
<Age>17</Age>
</Student>
<Student ID="2">
<Name>FONG</Name>
<Age>17</Age>
</Student>
<Student ID="2">
<Name>QEE</Name>
<Age>15</Age>
</Student>
</Class>

<Class ID="2" Name="ART">
<Student ID="1">
<Name>BOB</Name>
<Age>16</Age>
</Student>
<Student ID="2">
<Name>JACK</Name>
<Age>15</Age>
</Student>
<Student ID="3">
<Name>TOM</Name>
<Age>16</Age>
</Student>
<Student ID="4">
<Name>JERRY</Name>
<Age>15</Age>
</Student>
</Class>
</Root>

 

 

Students.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
DataSet ds
= new DataSet();
ds.ReadXml(MapPath(
"Students.xml"));

Response.Write(ds.Tables.Count
+ "<br />");
Response.Write(
"==========<br />");

for (int i = 0; i < ds.Tables.Count; i++)
{
//Response.Write(ds.Tables[i] + "<br />");

DataRow[] rows
= ds.Tables[i].Select();
for (int j = 0; j < rows.Length; j++)
{
Response.Write(rows[j][
"ID"] + " - " + rows[j]["Name"] + "<br />");
}

Response.Write(
"==========<br />");
}
}
}
</script>

 

Return

2
==========
1 - MUSIC
2 - ART
==========
1 - LEE
2 - WANG
2 - FONG
2 - QEE
1 - BOB
2 - JACK
3 - TOM
4 - JERRY
==========

 

我的疑问是,这样的XML结构返回的了两个 TABLE, 且似乎失去了层级关系。若是我只想取出 CLASS NAME 为 MUSIC 下的学生资料该怎么办?
我不想将 <Student> 改成 <Student ClassID="1"> 这种冗余的形式。这种形式可以用:ds.Tables[1].Select("CalssID=1");来获取我想要的 STUDENTS 集合,但失去了 XML 结构的意义

 

不知道我的问题改怎么解决

兄弟我实在太穷,只能裸求答案了...

 

 

burning的主页 burning | 初学一级 | 园豆:0
提问于:2010-08-24 15:42
< >
分享
所有回答(2)
0

ds.Tables[1].Select("Class_Id = 0");

不用你将 <Student> 改成 <Student ClassID="1"> 这种冗余的形式,生成的 DataTable 中自动包含外键,

也就是说ds.Tables[0] 和 ds.Tables[1] 都会自动添加 Class_Id 列.

Launcher | 园豆:45045 (高人七级) | 2010-08-24 16:21
0

除了Galactica提供的答案,你也可以了解下timyxml等第三方开源架构


邀月 | 园豆:25475 (高人七级) | 2010-08-24 17:22
好的,也去学习一下
支持(0) 反对(0) burning | 园豆:0 (初学一级) | 2010-08-25 15:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册