急救?请进来看看

jerrie_1 2005-07-25 03:54:09
我想把xml文件中的内容读到datagrid中去,请问如何实现 ?

authors.xml:
<Authors>
<Author>
<name>Mark</name>
<price>2200</price>
</Author>
<Author>
<name>Jane</name>
<price>3500</price>
</Author>
<Author>
<name>Tom</name>
<price>4000</price>
</Author>
</Authors>
...全文
241 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ctidea_binzi 2005-07-25
  • 打赏
  • 举报
回复
哈哈~~好热心~~
jerrie_1 2005-07-25
  • 打赏
  • 举报
回复
多谢你们哒
出来啦!
tigerwen01 2005-07-25
  • 打赏
  • 举报
回复
用ReadXml把文件读到数据集里,然后再和datagrid绑定:

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>

<html>

<script language="C#" runat="server">

protected void Page_Load(Object Src, EventArgs E) {
DataSet ds = new DataSet();

FileStream fs = new FileStream(Server.MapPath("authors.xml"),FileMode.Open,FileAccess.Read);
StreamReader reader = new StreamReader(fs);
ds.ReadXml(reader);
fs.Close();

DataView Source = new DataView(ds.Tables[0]);

MyLiteral.Text = Source.Table.TableName;
MyDataGrid.DataSource = Source;
MyDataGrid.DataBind();
}

</script>

<body>

<h3><font face="宋体">表的 XML 数据:<asp:Literal id="MyLiteral" runat="server" /></font></h3>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="900"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="宋体"
Font-Size="9pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>

</body>
</html>
jerrie_1 2005-07-25
  • 打赏
  • 举报
回复
我的代码是这样,来看看分析:
XmlDocument xml_xdoc=new XmlDocument ();
xml_xdoc.Load (Server.MapPath ("authors.xml"));
XmlNode xml_node=xml_xdoc.DocumentElement;
DataSet tempds=new DataSet ();
if (xml_node.NodeType ==XmlNodeType.Text )
{
XmlElement el = (XmlElement)xml_xdoc.SelectSingleNode("name");
tempds.ReadXml(el.InnerText);
}
this.DataGrid1.DataSource =tempds ;
this.DataGrid1.DataBind ();

错误消息如下:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code
System.Web.HttpException: The IListSource does not contain any data sources
错误指向:this.DataGrid1.DataBind ();
为什么?
helimin19 2005-07-25
  • 打赏
  • 举报
回复
DataSet ds = new DataSet();
ds.ReadXml(xml文件);
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
  • 打赏
  • 举报
回复
public static DataSet ReadXml(string strPath)
{
DataSet ds = new DataSet();
ds.ReadXml(strPath);
return ds;
}
smoothwood 2005-07-25
  • 打赏
  • 举报
回复
用dataset.ReadXml把这个文件对到数据集里边,然后再和datagrid绑定

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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