XML io流 解析,高手指教

来源:百度知道 编辑:UC知道 时间:2024/05/24 20:17:25
最近刚学这个,有点不太明白,请高手指教下
xml:
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book id="b001" >
<Title>C#高级编程</Title>
<Author>张三</Author>
<Publish>人民出版社1</Publish>
</Book>
<Book id="b002">
<Title>JAVA高级编程</Title>
<Author>王五</Author>
<Publish>人民出版社2</Publish>
</Book>
<Book id="b003">
<Title>PHP高级编程</Title>
<Author>李四</Author>
<Publish>人民出版社3</Publish>
</Book>
</Books>
reader.cs
public class Reader
{
string filePath="";
public Reader(string xmlFile)
{this.filePath = xmlFile;}
public string[] ReadNodes(string nodeName)
{
string[] results = new string[0];

using System.Xml;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNodeList booksList = xmlDoc.SelectNodes("/Books/Book");
for (int i = 0; i < booksList.Count; i++)
{
Response.Write(booksList[i].Attributes["id"].Value);
Response.Write(booksList[i].SelectSingleNode("Title").InnerText);
Response.Write(booksList[i].SelectSingleNode("Author").InnerText);
Response.Write(booksList[i].SelectSingleNode("Publish").InnerText);
}

XmlNodeList list = xmlDoc.GetElementsByTagName("Book");
foreach (XmlNode node in list)
{
Response.Write(node["Title"].InnerText + "," + node["Author"].InnerText + "," + node["Publish"].InnerText + "<br />");