老鼠划猫须

xml的第一次接触

xml里面的东西几乎都是自己写的,标签也是自己动手写。而且这东西还区分大小写的,要留点心了。

先给个例子瞅瞅:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <books>
 3   <book>
 4     <author>张三</author>
 5     <publisher>清华大学出版社</publisher>
 6     <date>2009-1-1</date>
 7     <name>ASP.NET实训教程</name>
 8     <isbn>978-7-333-20981-4</isbn>
 9     <price>50.00</price>
10   </book>
11   <book>
12     <author>李四</author>
13     <publisher>机械工业出版社</publisher>
14     <date>2009-6-8</date>
15     <name>ASP.NET详解</name>
16     <isbn>978-7-333-20332-1</isbn>
17     <price>45.00</price>
18   </book>
19   <book>
20     <author>王五</author>
21     <publisher>电子工业出版社</publisher>
22     <date>2010-10-9</date>
23     <name>C#程序设计</name>
24     <isbn>978-7-311-21231-2</isbn>
25     <price>50.00</price>
26   </book>
27   <book>
28     <author>张飞</author>
29     <publisher>机械工业出版社</publisher>
30     <date>2008-4-12</date>
31     <name>.NET设计模式</name>
32     <isbn>978-7-310-12341-9</isbn>
33     <price>30.00</price>
34   </book>
35 </books>

 

以上就是一个xml里面的东西。

这里面的标记(books、book、author...)都是自己写的,这些标记有开始就要有结束。

xml也是有验证的。

 

在后台调用Xml的时候:

 1  XmlReaderSettings settings = new XmlReaderSettings();
 2             settings.IgnoreComments = true;//忽略Xml中的注释
 3             settings.IgnoreWhitespace = true;//忽略没必要的空白
 4 
 5             using (XmlReader reader = XmlReader.Create(Server.MapPath("books.xml"), settings))//第一个参数需要的是实路径
 6             {
 7                 int booknum = 0;
 8                 while (reader.Read())
 9                 {
10                     if (reader.NodeType == XmlNodeType.Element)
11                     {
12                         if (reader.LocalName == "book")
13                         {
14                             booknum++;
15                         }
16                     }
17                 }
18                 Response.Write(booknum);
19             }
20 
21             XmlWriterSettings settingsw=new XmlWriterSettings();
22             settingsw.Encoding=System.Text.Encoding.UTF8;//编码类型
23             settingsw.Indent=true;
24             using (XmlWriter writere = XmlWriter.Create(Server.MapPath("newbook.xml"), settingsw))
25             {
26                 writere.WriteStartDocument();//开始写版本以及链接
27 
28                 writere.WriteStartElement("books");//根节点
29 
30                 writere.WriteStartElement("book");//book节点开始
31 
32                 writere.WriteStartAttribute("id");//给book添加属性  属性名为id
33                 writere.WriteValue("1");//写入属性值
34                 writere.WriteEndAttribute();//属性编辑结束
35 
36                 writere.WriteStartElement("author");//author节点开始
37                 writere.WriteString("乔布斯");//写入author节点内容
38                 writere.WriteEndElement();//结束author节点
39 
40                 writere.WriteEndElement();//结束book节点
41 
42                 writere.WriteStartElement("book");//book节点开始
43                 writere.WriteStartElement("author");//author节点开始
44                 writere.WriteString("布鲁斯");//写入author节点内容
45                 writere.WriteEndElement();//结束author节点
46                 writere.WriteEndElement();//结束book节点
47 
48                 writere.WriteEndElement();//结束books节点
49                 writere.WriteEndDocument();//结束
50             }

上面的就是Xml文件的读取和写入了。

posted on 2012-12-20 11:06  昔日流年  阅读(159)  评论(0)    收藏  举报

导航