C#对XML文档的追加和读取(包括documen和XmlTextreader读取)

xml文件

C#对XML文档的追加和读取(包括documen和XmlTextreader读取)_第1张图片

 c#源码:

using System.Xml;
using System.Xml.XPath;
using System.Text;

class program
{
   
   // name, ISBN, author, price, publicer;
   
   public static void  Main()
    {
        tj xinjia = new tj();
        xinjia.xmltj();//追加书籍信息
       xmltext text = new xmltext();
       text.readbook();//document的读取
        //xmlread2 text2 = new xmlread2();//流模式的读取
       // text2.xmlliu();
    }
}
class xmltext
{
    public void readbook()
    {
        Console.WriteLine("document的读取");
        XmlDocument xmlDoc = new XmlDocument();
        string BookISBN;
        xmlDoc.Load(@path);
        XmlNode xn = xmlDoc.SelectSingleNode("note");

        XmlNodeList xnl = xn.ChildNodes;

        foreach (XmlNode xn1 in xnl)
        {

            XmlElement xe = (XmlElement)xn1;
            // 得到Type和ISBN两个属性的属性值
            BookISBN = xe.GetAttribute("ISBN").ToString();

            // 得到Book节点的所有子节点
            string BookName, BookAuthor, BookPrice,publicer;
            XmlNodeList xnl0 = xe.ChildNodes;
            BookName = xnl0.Item(0).InnerText;
            BookAuthor = xnl0.Item(1).InnerText;
            BookPrice = xnl0.Item(2).InnerText;
            publicer = xnl0.Item(3).InnerText;
            Console.WriteLine("书籍信息ISBN:{0},书名{1} 作者{2} 价格{3} 出版社{4}", BookISBN, BookName, BookAuthor, BookPrice,publicer);
        }

        Console.ReadKey();
    }
}
class book
{
    public string name,ISBN,price,author,publicer;
}
class xmlread2
{
    public void xmlliu()
    {
        XmlTextReader reader = new XmlTextReader(@"D:\\vs2022 project\\ConsoleApp14\\XMLFile1.xml");
        book obj = new book();
        //reader.Read();
        Console.WriteLine("流模式读取");
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {


                if (reader.Name == "bookstore")
                {
                    Console.Write(reader.ReadString());
                }

                if (reader.Name == "name")
                {
                    Console.Write(reader.ReadString());
                }
                if (reader.Name == "author")
                {
                    Console.Write(reader.ReadString());
                }
                if (reader.Name == "price")
                {
                    Console.Write(reader.ReadString());
                }
                if (reader.Name == "publicer")
                {
                    Console.WriteLine(reader.ReadString());
                }               

            }
           
        }
    }
}
class tj
{
    public void xmltj()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement bookstore;
        if (File.Exists(path))
        {
            doc.Load(path);
            bookstore = doc.DocumentElement;
        }
        else
        {
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            bookstore = doc.CreateElement("bookstore");
            doc.AppendChild(bookstore);
        }
        XmlElement book1 = doc.CreateElement("bookstore");
        bookstore.AppendChild(book1);
        book1.SetAttribute("ISBN", "10013");

        XmlElement name = doc.CreateElement("name");
        name.InnerText = "《飘》";
        book1.AppendChild(name);

        XmlElement author = doc.CreateElement("author");
        author.InnerText = "玛格丽特.米切尔";
        book1.AppendChild(author);

        XmlElement price = doc.CreateElement("price");
        price.InnerText = "30";
        book1.AppendChild(price);

        XmlElement publicer = doc.CreateElement("publicer");
        publicer.InnerText = "译林出版社";
        book1.AppendChild(publicer);

        doc.Save(path);
        Console.WriteLine("保存信息成功!");
    }
}

路径有问题的前面加个@

缺少引用的添加system.io......

运行过程

C#对XML文档的追加和读取(包括documen和XmlTextreader读取)_第2张图片

xml的变化:

C#对XML文档的追加和读取(包括documen和XmlTextreader读取)_第3张图片

你可能感兴趣的:(xml,c#,开发语言)