2007年8月9日 星期四

讀取 XML 檔案

Ori.xml  程式碼:
-------------------------------------------------
<Root>

<Book>

<BookID>1</BookID>

<BookName>Good More</BookName>

<BookPrice>38</BookPrice>

<BookSale>40</BookSale>

</Book>

<Book>

<BookID>2</BookID>

<BookName>Good Better</BookName>

<BookPrice>55</BookPrice>

<BookSale>60</BookSale>

</Book>

<Book>

<BookID>3</BookID>

<BookName>Good Peace</BookName>

<BookPrice>25</BookPrice>

<BookSale>30</BookSale>

</Book>

</Root>
 
讀取 XML 的範例:
  private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DataSet ds=null;
string xmlFilename=null;
if(!IsPostBack)
{
try
{
xmlFilename=Server.MapPath(".")+"\\ori.xml";
ds=new DataSet();
ds.ReadXml(xmlFilename);
DataGrid1.DataSource=ds.Tables["Book"];
DataGrid1.DataBind();
}

catch
{
}

finally
{
if(ds!=null)
ds.Dispose();
}
}
}

沒有留言:

張貼留言