操作XML

操作XML

using System;
using System.IO;
using System.Xml;
using System.Text;

public class xml
{
    
public static void Main(String [] Args)
    {
        
try
        {
            
string fileName="booknew.xml";

            XmlTextWriter tw
=new XmlTextWriter(fileName,null);
            
            tw.Formatting
=Formatting.Indented;
            
            tw.WriteStartDocument();
                tw.WriteStartElement(
"book");
                    tw.WriteAttributeString(
"genre","Mystery");
                    tw.WriteAttributeString(
"ISBN","123456789");
                    tw.WriteElementString(
"title","Case of the Missing Cookie");
                    tw.WriteStartElement(
"author");
                        tw.WriteElementString(
"name","Cookie Monster");
                    tw.WriteEndElement();
                    tw.WriteElementString(
"price","9.99");
                tw.WriteEndElement();
            tw.WriteEndDocument();
            
            tw.Flush();
            tw.Close();
        }
        
catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }
}
posted @ 2008-07-18 23:12  angushine  阅读(111)  评论(0编辑  收藏  举报