首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

C#读取xml数据并开展分页 !

2011-12-26 
C#读取xml数据并进行分页 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!C#读取xml数据并进行分页!!!!!!!!!!有

C#读取xml数据并进行分页 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C#读取xml数据并进行分页   !!!!!!!!!!

有例子更好,谢谢大家了!!!!!!!!!!

[解决办法]
读取相同目录下的XML文件
private void loadData()
{
DataSet mydataset = new DataSet();
//读取xml文件
mydataset.ReadXml(Server.MapPath( "databackup.xml "));
//grid.DataSource = mydataset.Tables[0];
//grid.DataBind();
}
[解决办法]
namespace admin
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

/// <summary>
///AdminMenu 的摘要说明。
/// </summary>
public class AdminMenu : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataList MyList;

private void Page_Load(object sender, System.EventArgs e)
{
string selectionID = Request.Params[ "selection "];

if ( selectionID != null)
{
MyList.SelectedIndex = Int32.Parse(selectionID);
}
BindMenu(Session[ "RoleId "].ToString());

}

/// <summary>
/// 从XML文件读入管理员目录的列表项目,并绑定到目录
/// </summary>
private void BindMenu(string roleId)
{
System.Xml.XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath( "AdminMenu.xml "));

XmlNode itemNode = doc.DocumentElement;


DataTable menuList = new DataTable();

menuList.Columns.Add( "ItemName ");
menuList.Columns.Add( "ItemUrl ");

string itemName, itemUrl, itemRole;
string []rowItem = new string[2];

foreach (XmlNode node in itemNode)
{
itemRole = node.Attributes[ "RoleId "].Value;
if (roleId == "2 ")
{
if ( itemRole == roleId)
{
itemName = node.Attributes[ "ItemName "].Value;
itemUrl = node.Attributes[ "ItemUrl "].Value;

rowItem[0] = itemName;
rowItem[1] = itemUrl;

menuList.Rows.Add(rowItem);
}
}
else
{
itemName = node.Attributes[ "ItemName "].Value;
itemUrl = node.Attributes[ "ItemUrl "].Value;

rowItem[0] = itemName;
rowItem[1] = itemUrl;

menuList.Rows.Add(rowItem);
}


}

MyList.DataSource = menuList.DefaultView;
MyList.DataBind();


}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
///设计器支持所需的方法 - 不要使用代码编辑器
///修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

[解决办法]
<html>
<head>
<script type= "text/javascript ">
function movenext()
{
x=xmldso.recordset
if (x.absoluteposition < x.recordcount)
{
x.movenext()
}
}
function moveprevious()
{
x=xmldso.recordset
if (x.absoluteposition > 1)


{
x.moveprevious()
}
}
</script>
</head>
<body>
<xml src= "cd_catalog.xml " tppabs= "http://www.w3schools.com/xml/cd_catalog.xml " id= "xmldso " async= "false "> </xml>
<p>
Title:
<span datasrc= "#xmldso " datafld= "TITLE "> </span>
<br /> Artist:
<span datasrc= "#xmldso " datafld= "ARTIST "> </span>
<br /> Year:
<span datasrc= "#xmldso " datafld= "YEAR "> </span>
</p>
<p>
<input type= "button " value= "Previous CD "
onclick= "moveprevious() " />
<input type= "button " value= "Next CD "
onclick= "movenext() " />
</p>
</body>
</html>



[解决办法]
<?xml version= "1.0 " encoding= "ISO-8859-1 "?>
<!-- Edited with XML Spy v4.2 -->
<CATALOG>
<CD>
<TITLE> Empire Burlesque </TITLE>
<ARTIST> Bob Dylan </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> Columbia </COMPANY>
<PRICE> 10.90 </PRICE>
<YEAR> 1985 </YEAR>
</CD>
<CD>
<TITLE> Hide your heart </TITLE>
<ARTIST> Bonnie Tyler </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> CBS Records </COMPANY>
<PRICE> 9.90 </PRICE>
<YEAR> 1988 </YEAR>
</CD>
<CD>
<TITLE> Greatest Hits </TITLE>
<ARTIST> Dolly Parton </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> RCA </COMPANY>
<PRICE> 9.90 </PRICE>
<YEAR> 1982 </YEAR>
</CD>
<CD>
<TITLE> Still got the blues </TITLE>
<ARTIST> Gary Moore </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Virgin records </COMPANY>
<PRICE> 10.20 </PRICE>
<YEAR> 1990 </YEAR>
</CD>
<CD>
<TITLE> Eros </TITLE>
<ARTIST> Eros Ramazzotti </ARTIST>
<COUNTRY> EU </COUNTRY>
<COMPANY> BMG </COMPANY>
<PRICE> 9.90 </PRICE>
<YEAR> 1997 </YEAR>
</CD>
<CD>
<TITLE> One night only </TITLE>
<ARTIST> Bee Gees </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Polydor </COMPANY>
<PRICE> 10.90 </PRICE>
<YEAR> 1998 </YEAR>
</CD>
<CD>
<TITLE> Sylvias Mother </TITLE>
<ARTIST> Dr.Hook </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> CBS </COMPANY>
<PRICE> 8.10 </PRICE>
<YEAR> 1973 </YEAR>
</CD>
<CD>
<TITLE> Maggie May </TITLE>
<ARTIST> Rod Stewart </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Pickwick </COMPANY>
<PRICE> 8.50 </PRICE>
<YEAR> 1990 </YEAR>
</CD>
<CD>
<TITLE> Romanza </TITLE>
<ARTIST> Andrea Bocelli </ARTIST>


<COUNTRY> EU </COUNTRY>
<COMPANY> Polydor </COMPANY>
<PRICE> 10.80 </PRICE>
<YEAR> 1996 </YEAR>
</CD>
<CD>
<TITLE> When a man loves a woman </TITLE>
<ARTIST> Percy Sledge </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> Atlantic </COMPANY>
<PRICE> 8.70 </PRICE>
<YEAR> 1987 </YEAR>
</CD>
<CD>
<TITLE> Black angel </TITLE>
<ARTIST> Savage Rose </ARTIST>
<COUNTRY> EU </COUNTRY>
<COMPANY> Mega </COMPANY>
<PRICE> 10.90 </PRICE>
<YEAR> 1995 </YEAR>
</CD>
<CD>
<TITLE> 1999 Grammy Nominees </TITLE>
<ARTIST> Many </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> Grammy </COMPANY>
<PRICE> 10.20 </PRICE>
<YEAR> 1999 </YEAR>
</CD>
<CD>
<TITLE> For the good times </TITLE>
<ARTIST> Kenny Rogers </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Mucik Master </COMPANY>
<PRICE> 8.70 </PRICE>
<YEAR> 1995 </YEAR>
</CD>
<CD>
<TITLE> Big Willie style </TITLE>
<ARTIST> Will Smith </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> Columbia </COMPANY>
<PRICE> 9.90 </PRICE>
<YEAR> 1997 </YEAR>
</CD>
<CD>
<TITLE> Tupelo Honey </TITLE>
<ARTIST> Van Morrison </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Polydor </COMPANY>
<PRICE> 8.20 </PRICE>
<YEAR> 1971 </YEAR>
</CD>
<CD>
<TITLE> Soulsville </TITLE>
<ARTIST> Jorn Hoel </ARTIST>
<COUNTRY> Norway </COUNTRY>
<COMPANY> WEA </COMPANY>
<PRICE> 7.90 </PRICE>
<YEAR> 1996 </YEAR>
</CD>
<CD>
<TITLE> The very best of </TITLE>
<ARTIST> Cat Stevens </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Island </COMPANY>
<PRICE> 8.90 </PRICE>
<YEAR> 1990 </YEAR>
</CD>
<CD>
<TITLE> Stop </TITLE>
<ARTIST> Sam Brown </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> A and M </COMPANY>
<PRICE> 8.90 </PRICE>
<YEAR> 1988 </YEAR>
</CD>
<CD>
<TITLE> Bridge of Spies </TITLE>
<ARTIST> T 'Pau </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Siren </COMPANY>
<PRICE> 7.90 </PRICE>
<YEAR> 1987 </YEAR>
</CD>
<CD>
<TITLE> Private Dancer </TITLE>
<ARTIST> Tina Turner </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> Capitol </COMPANY>
<PRICE> 8.90 </PRICE>
<YEAR> 1983 </YEAR>
</CD>
<CD>
<TITLE> Midt om natten </TITLE>
<ARTIST> Kim Larsen </ARTIST>
<COUNTRY> EU </COUNTRY>


<COMPANY> Medley </COMPANY>
<PRICE> 7.80 </PRICE>
<YEAR> 1983 </YEAR>
</CD>
<CD>
<TITLE> Pavarotti Gala Concert </TITLE>
<ARTIST> Luciano Pavarotti </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> DECCA </COMPANY>
<PRICE> 9.90 </PRICE>
<YEAR> 1991 </YEAR>
</CD>
<CD>
<TITLE> The dock of the bay </TITLE>
<ARTIST> Otis Redding </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> Atlantic </COMPANY>
<PRICE> 7.90 </PRICE>
<YEAR> 1987 </YEAR>
</CD>
<CD>
<TITLE> Picture book </TITLE>
<ARTIST> Simply Red </ARTIST>
<COUNTRY> EU </COUNTRY>
<COMPANY> Elektra </COMPANY>
<PRICE> 7.20 </PRICE>
<YEAR> 1985 </YEAR>
</CD>
<CD>
<TITLE> Red </TITLE>
<ARTIST> The Communards </ARTIST>
<COUNTRY> UK </COUNTRY>
<COMPANY> London </COMPANY>
<PRICE> 7.80 </PRICE>
<YEAR> 1987 </YEAR>
</CD>
<CD>
<TITLE> Unchain my heart </TITLE>
<ARTIST> Joe Cocker </ARTIST>
<COUNTRY> USA </COUNTRY>
<COMPANY> EMI </COMPANY>
<PRICE> 8.20 </PRICE>
<YEAR> 1987 </YEAR>
</CD>
</CATALOG>

[解决办法]
最简单的方法是利用xmldatasource
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(xml地址);
XmlDataSource1.Data = doc.InnerXml;

对于xmldatasource
<asp:XmlDataSource ID= "XmlDataSource1 " runat= "server " XPath= "/rss/channel/item[position() <4] "> </asp:XmlDataSource>
其中xpatch是格式~见msdn这里的意思是取出前3条

之后你数据绑定就可以用了


<asp:Repeater ID= "Repeater1 " runat= "server " DataSourceID= "XmlDataSource1 ">
<ItemTemplate>
<div align= "left "> &nbsp;&nbsp;
<asp:HyperLink ID= "HyperLink1 " runat= "server " CssClass= "word " NavigateUrl= ' <%#XPath( "link ") %> '
Target= "_blank ">
<%#getword(XPath( "title "))%> </asp:HyperLink> </div>
</ItemTemplate>
</asp:Repeater>

热点排行