回覆列表
  • 1 # 使用者9937787127551

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Xml;

    using System.IO;

    namespace WriteXml

    {

    class Program

    {

    static void Main(string[] args)

    {

    //createXml();//建立檔案和新增基本資料,便於之後操作實驗

    //addItem();//追加資料

    readtext();//讀取資料

    //updatexml();//更新資料

    }

    private static void createXml()

    {

    XmlTextWriter writer = new XmlTextWriter("titles.xml", null);

    //使用自動縮排便於閱讀

    writer.Formatting = Formatting.Indented;

    //寫入根元素

    writer.WriteStartElement("items");

    writer.WriteStartElement("item");

    //寫入屬性及屬性的名字

    writer.WriteAttributeString("類別", "文學");

    writer.WriteAttributeString("品質", "優");

    //加入子元素

    writer.WriteElementString("title", "毛著");

    writer.WriteElementString("author", "毛澤東");

    writer.WriteElementString("price", "10.0");

    //關閉根元素,並書寫結束標籤

    writer.WriteEndElement();

    writer.WriteEndElement();

    //將XML寫入檔案並且關閉XmlTextWriter

    writer.Close();

    }

    private static void addItem()

    {

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("titles.xml");

    XmlNode root = xmlDoc.SelectSingleNode("items");

    XmlElement xe1 = xmlDoc.createElement_x("item");

    xe1.SetAttribute("類別", "張三");

    xe1.SetAttribute("品質", "老師");

    XmlElement xmlsub1 = xmlDoc.createElement_x("title");

    xmlsub1.InnerText = "wahahha";

    xe1.AppendChild(xmlsub1);

    XmlElement xmlsub2 = xmlDoc.createElement_x("author");

    xmlsub2.InnerText = "三毛";

    xe1.AppendChild(xmlsub2);

    XmlElement xmlsub3 = xmlDoc.createElement_x("Price");

    xmlsub3.InnerText = "15.00";

    xe1.AppendChild(xmlsub3);

    root.AppendChild(xe1);

    xmlDoc.Save("titles.xml");

    }

    private static void readtext()

    {

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("titles.xml");

    XmlNode xn = xmlDoc.SelectSingleNode("items");

    XmlNodeList xnl=xn.ChildNodes;

    foreach (XmlNode xnf in xnl)

    {

    XmlElement xe = (XmlElement)xnf;

    Console.Write("類別 "+xe.GetAttribute("類別")+" 品質 ");//顯示屬性值

    Console.WriteLine(xe.GetAttribute("品質"));

    XmlNodeList xnf1 = xe.ChildNodes;

    foreach (XmlNode xn2 in xnf1)

    {

    Console.WriteLine(xn2.InnerText);//顯示子節點點文字

    }

    Console.WriteLine();

    }

    }

    private static void updatexml()

    {

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("titles.xml");

    XmlNode xns = xmlDoc.SelectSingleNode("items");

    XmlNodeList xnl = xns.ChildNodes;

    foreach (XmlNode xn in xnl)

    {

    XmlElement xe = (XmlElement)xn;

    if (xe.GetAttribute("類別") == "文學")

    {

    xe.SetAttribute("類別","娛樂") ;

    }

    else

    {

    xe.SetAttribute("類別", "文學");

    }

    XmlNodeList xnl2 = xe.ChildNodes;

    foreach (XmlNode xn2 in xnl2)

    {

    XmlElement xe2 = (XmlElement)xn2;

    if (xe2.Name=="price")

    {

    if (xe2.InnerText == "10.00")

    xe2.InnerText = "15.00";

    else

    xe2.InnerText = "10.00";

    }

    //break;

    }

    //break;

    }

    xmlDoc.Save("titles.xml");

    Console.WriteLine();

    Console.WriteLine();

    Console.WriteLine();

    Console.WriteLine();

    readtext();

    }

    private static void delnode()

    {

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("titles.xml");

    XmlNodeList xnl = xmlDoc.SelectSingleNode("items").ChildNodes;

    foreach (XmlNode xn in xnl)

    {

    XmlElement xe = (XmlElement)xn;

    if (xe.GetAttribute("類別") == "文學")

    {

    }

    else if (xe.GetAttribute("類別") == "娛樂")

    {

    }

    }

    xmlDoc.Save("titles.xml");

    Console.WriteLine();

    Console.WriteLine();

    Console.WriteLine();

    Console.WriteLine();

    readtext();

    }

    }

    }

  • 中秋節和大豐收的關聯?
  • 紅茶真的暖胃嗎?