Chinaunix首页 | 论坛 | 博客
  • 博客访问: 756328
  • 博文数量: 96
  • 博客积分: 2023
  • 博客等级: 上尉
  • 技术积分: 1738
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-15 10:03
文章分类

全部博文(96)

文章存档

2014年(11)

2012年(85)

分类: 嵌入式

2012-05-25 22:27:28

1.源代码

点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;

  6. namespace XMLApplication
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             XmlDocument xmlDoc = new XmlDocument();
  13.             //加入XML的声明段落,Save方法不再xml上写出独立属性

  14.             xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
  15.             //加入根元素

  16.             XmlElement xmlElem = xmlDoc.CreateElement("", "Books", "");
  17.             xmlDoc.AppendChild(xmlElem);
  18.             //FileName

  19.             XmlElement xmlElemFileName = xmlDoc.CreateElement("FileName");
  20.             XmlText xmlTextFileName = xmlDoc.CreateTextNode("书籍文件列表");
  21.             xmlElemFileName.AppendChild(xmlTextFileName);
  22.             xmlDoc.ChildNodes.Item(1).AppendChild(xmlElemFileName);

  23.             //UserInfo

  24.             XmlElement xmlElemUserInfo = xmlDoc.CreateElement("UserInfo");
  25.             xmlDoc.ChildNodes.Item(1).AppendChild(xmlElemUserInfo);
  26.             //UserName

  27.             XmlElement xmlElemUsrName = xmlDoc.CreateElement("UserName");
  28.             XmlText xmlTextUserName = xmlDoc.CreateTextNode("Arlice");//用户名

  29.             xmlElemUsrName.AppendChild(xmlTextUserName);
  30.             xmlDoc.ChildNodes.Item(1).ChildNodes[1].AppendChild(xmlElemUsrName);

  31.             //ChineseBook

  32.             XmlElement xmlElemChineseBook = xmlDoc.CreateElement("ChineseBook");
  33.             xmlDoc.ChildNodes.Item(1).AppendChild(xmlElemChineseBook);
  34.             //Book1

  35.             XmlElement xmlElemBook1 = xmlDoc.CreateElement("Book1");
  36.             xmlElemBook1.SetAttribute("BookCode", "001");
  37.             xmlDoc.ChildNodes.Item(1).ChildNodes[2].AppendChild(xmlElemBook1);
  38.             //BookName

  39.             XmlElement xmlElemBookName = xmlDoc.CreateElement("BookName");
  40.             XmlText xmlTextBookName = xmlDoc.CreateTextNode("小学语文");//书名

  41.             xmlElemBookName.AppendChild(xmlTextBookName);
  42.             xmlDoc.ChildNodes.Item(1).ChildNodes[2].ChildNodes[0].AppendChild(xmlElemBookName);
  43.             //publishing house

  44.             XmlElement xmlElemPh = xmlDoc.CreateElement("PublishingHouse");
  45.             XmlText xmlTextPh = xmlDoc.CreateTextNode("电子工业出版社");
  46.             xmlElemPh.AppendChild(xmlTextPh);
  47.             xmlDoc.ChildNodes.Item(1).ChildNodes[2].ChildNodes[0].AppendChild(xmlElemPh);
  48.             try
  49.             {
  50.                 xmlDoc.Save("f:\\cc.xml");
  51.             }
  52.             catch (System.Exception ex)
  53.             {
  54.                 Console.WriteLine(ex.Message);
  55.             }
  56.             Console.ReadLine();
  57.         }
  58.     }
  59. }

2.运行结果

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Books>
  3.   <FileName>书籍文件列表</FileName>
  4.   <UserInfo>
  5.     <UserName>Arlice</UserName>
  6.   </UserInfo>
  7.   <ChineseBook>
  8.     <Book1 BookCode="001">
  9.       <BookName>小学语文</BookName>
  10.       <PublishingHouse>电子工业出版社</PublishingHouse>
  11.     </Book1>
  12.   </ChineseBook>
  13. </Books>

阅读(4290) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~