xml基础练习
来源:互联网 发布:java eclipse使用教程 编辑:程序博客网 时间:2024/05/17 22:48
1使用XlmReader读books.xml中有几本书。
--------Demo.aspx.cs---------
protected void Page_Load(object sender, EventArgs e)
{
XmlReaderSettings settings=new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
int booknum = 0;
using (XmlReader reader = XmlReader.Create(Server.MapPath("books.xml"), settings))//只要需要的路径不是url形式,就需要写实际的路径,不能写虚路径
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName == "book")
{
booknum++;
}
}
}
}
Response.Write("一共找到了"+booknum+"本书");
}
2使用XlmWriter写NewBooks.xml文件。
----Demo1.aspx-----
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click"
Text="WriteXml" />
</form>
</body>
----Demo1.aspx.cs-----
protected void Button1_Click(object sender, EventArgs e)
{
XmlWriterSettings settings=new XmlWriterSettings();
settings.Encoding=System.Text.Encoding.UTF8;
settings.Indent=true;
using (XmlWriter write = XmlWriter.Create(Server.MapPath("newbook.xml"), settings))
{
write.WriteStartDocument();//开始写<?xml version="1.0" encoding="utf-8"?>
write.WriteStartElement("books");//写根节点
write.WriteStartElement("book");//book节点开始
write.WriteStartAttribute("id");//给boook添加属性
write.WriteValue("1");//设置属性id的值为1
write.WriteEndAttribute();//结束属性id的设置
write.WriteStartElement("author");//author节点开始
write.WriteString("join");//author内容
write.WriteEndElement();//author节点结束
write.WriteEndElement();//book节点技术
//第二本书开始
write.WriteStartElement("book");//book节点开始
write.WriteStartElement("publiser");//author节点开始
write.WriteString("清华大学出版社");//author内容
write.WriteEndElement();//author节点结束
write.WriteEndElement();//book节点技术
}
Response.Write("ok");
}
3使用XlmWrite把一个xml文件写入输出流中。在(demo2.aspx运行时显示xml文档)
-------Demo2.aspx---------
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="Demo2.aspx.cs" Inherits="XML.Demo2" %>
-------Demo2.aspx.cs---------
namespace XML
{
public partial class Demo2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = System.Text.Encoding.UTF8;
settings.Indent = true;
using (XmlWriter write = XmlWriter.Create(this.Response.Output, settings))
{
write.WriteStartDocument();//开始写<?xml version="1.0" encoding="utf-8"?>
write.WriteStartElement("books");//写根节点
write.WriteStartElement("book");//book节点开始
write.WriteStartAttribute("id");//给boook添加属性
write.WriteValue("1");//设置属性id的值为1
write.WriteEndAttribute();//结束属性id的设置
write.WriteStartElement("author");//author节点开始
write.WriteString("join");//author内容
write.WriteEndElement();//author节点结束
write.WriteEndElement();//book节点技术
//第二本书开始
write.WriteStartElement("book");//book节点开始
write.WriteStartElement("publiser");//author节点开始
write.WriteString("清华大学出版社");//author内容
write.WriteEndElement();//author节点结束
write.WriteEndElement();//book节点技术
}
}
}
}
4使用XmlDocument 读取books.xml中的书名到DropDownList中。(4-2选作使用XmlReader读取books.xml中的书名到DropDownList中)
-------Demo3.aspx----------
<div>
书名:<asp:DropDownList ID="DropDownList1" runat="server" Height="25px"
Width="184px">
</asp:DropDownList>
</div>
---------Demo3.aspx.cs------------
XmlDocument xdoc;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
xdoc = new XmlDocument();//声明一个xml文档实例,xdoc代表一个文档,但是代表谁现在没说
xdoc.Load(Server.MapPath("books.xml"));//把book这个xml文档copy一份给xdoc
XmlNodeList list = xdoc.GetElementsByTagName("name");
foreach (XmlNode node in list)//把里面的内容都出来,用遍历取出来?
{
this.DropDownList1.Items.Add(node.InnerText);
}
Session["doc"] = xdoc;
}
else
{
xdoc=Session["doc"] as XmlDocument;
}
}
5使用XmlDocument将dropdownlist中显示的书名的其它信息读入到相应的文本框中。
------------Demo4.aspx-----------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 141px;
}
.style2
{
width: 200px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
书名:<asp:DropDownList ID="DropDownList1" runat="server" Height="25px"
Width="184px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click" Text="显示详情" />
<br />
<table style="width:100%;">
<tr>
<td class="style1">
作者:</td>
<td class="style2">
<asp:TextBox ID="Txtauthor" runat="server" Width="199px"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
出版社:</td>
<td class="style2">
<asp:TextBox ID="Txtpublisher" runat="server" Width="198px"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
出版年月:</td>
<td class="style2">
<asp:TextBox ID="TxtYear" runat="server" Width="199px"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Isbn号:</td>
<td class="style2">
<asp:TextBox ID="TxtISbn" runat="server" Width="199px"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
价格:</td>
<td class="style2">
<asp:TextBox ID="txtprice" runat="server" Width="199px"></asp:TextBox>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
------------Demo4.aspx.cs-----------
namespace XML
{
public partial class Demo3 : System.Web.UI.Page
{
XmlDocument xdoc;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("books.xml"));
XmlNodeList list = xdoc.GetElementsByTagName("name");
foreach (XmlNode node in list)
{
this.DropDownList1.Items.Add(node.InnerText);
}
Session["doc"] = xdoc;
}
else
{
xdoc=Session["doc"] as XmlDocument;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
#region 方法1
foreach (XmlNode item in node.ChildNodes)
{
if (item.LocalName == "author")
{
this.Txtauthor.Text = item.InnerText;
}
if (item.LocalName == "publisher")
{
this.Txtpublisher.Text = item.InnerText;
}
if (item.LocalName == "date")
{
this.TxtYear.Text = item.InnerText;
}
if (item.LocalName == "isbn")
{
this.TxtISbn.Text = item.InnerText;
}
if (item.LocalName == "price")
{
this.txtprice.Text = item.InnerText;
}
}
#endregion
//#region 方法2
//Txtauthor.Text = node.SelectSingleNode("author").InnerText;
//Txtpublisher.Text = node.SelectSingleNode("publisher").InnerText;
//TxtYear.Text = node.SelectSingleNode("date").InnerText;
//TxtISbn.Text = node.SelectSingleNode("isbn").InnerText;
//txtprice.Text = node.SelectSingleNode("price").InnerText;
//#endregion
}
}
}
- xml基础练习
- 基础加强第五天 XML练习
- javaee之xml和dtd的基础练习
- XML练习
- 基础练习
- 基础练习
- 基础练习
- 基础练习
- 基础练习
- Java:基础练习-循环练习
- 【c基础练习】文件练习
- 使用SAX读取XML文件--(SAX的基础语法练习)
- XML练习(一)
- xml节点练习
- 【c基础练习】数组基础练习
- XML基础
- XML基础
- XML基础
- hibernate one-to-many many-to-one 双向注解
- 控件赋值时需要注意控件本身是否visible
- 悲催的程序员
- iOS安全:黑客与反黑客
- java 特性详解
- xml基础练习
- CvSeq 序列
- 字符集-Locale详解(续)
- 云计算:大数据时代的系统工程
- win7 IP 自动分配
- 安防产品采购注意事项
- [python]抓取2011年县及县以上行政区划代码
- Apple微软Google都在竞购一家创业公司,重点是专利
- 我眼中的程序化交易之路