C# XML,XmlDocument簡單操作實例


     private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml";

1.添加節點

/// <summary>
/// 向根節點中插入一個節點
/// </summary>
public static void AddOne()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(_Store);

    //1.查找booksotre節點
    XmlNode root = xmlDoc.SelectSingleNode("bookstore");
    //2.創建book 節點
    XmlElement book = xmlDoc.CreateElement("book");
    book.SetAttribute("genre", "lizanhong");
    book.SetAttribute("ISBN", "2-3431-4");
    XmlElement title = xmlDoc.CreateElement("title");
    title.InnerText = "C#入門經典";
    book.AppendChild(title);
    XmlElement author = xmlDoc.CreateElement("author");
    author.InnerText = "厚街";
    book.AppendChild(author);
    XmlElement price = xmlDoc.CreateElement("price");
    price.InnerText = "58.3";
    book.AppendChild(price);

    //將book節點,添加到根節點
    root.AppendChild(book);

    //保存內容
    xmlDoc.Save(_Store);
}

 

2.修改節點

/// <summary>
/// 修改節點
/// </summary>
public static void UpdateOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    //遍歷修改
    XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;
    foreach (XmlNode node in nodeList)
    {
        //將子節點類型轉換為XmlEletment類型
        XmlElement ele = (XmlElement)node;
        if (ele.GetAttribute("genre") == "lizanhong")
        {
            ele.SetAttribute("genre", "udpate禮贊紅");
            XmlNodeList nodeList2 = ele.ChildNodes;
            foreach (XmlNode node2 in nodeList2)
            {
                XmlElement ele2 = (XmlElement)node2;
                if (ele2.Name == "author")
                {
                    ele2.InnerText = "延納";
                    break;
                }
            }
            break;
        }
    }
    //保存修改
    doc.Save(_Store);
}
/// <summary>
/// 修改節點2,使用xpath
/// </summary>
public static void UpdateTwo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    //查詢節點
    //XmlNode root = doc.SelectSingleNode("bookstore");
    //XmlNodeList books = doc.SelectNodes("bookstore/book");
    XmlNode title = doc.SelectNodes("bookstore/book/title")[0];
    title.InnerText = title.InnerText + "---xpath";
    doc.Save(_Store);
}

3.刪除節點

/// <summary>
/// 刪除節點,屬性,內容
/// </summary>
public static void DeleteOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;
    foreach (var item in nodeList)
    {
        XmlElement ele = (XmlElement)item;
        if (ele.GetAttribute("genre") == "fantasy")
        {
            //刪除屬性
            ele.RemoveAttribute("genre");
        }
        else if (ele.GetAttribute("genre") == "udpate禮贊紅")
        {
            //刪除該節點的全部內容
            ele.RemoveAll();
        }
    }
    //保存修改
    doc.Save(_Store);
}
/// <summary>
/// 刪除空節點
/// </summary>
public static void DeleteTwo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);

    XmlNode root = doc.SelectSingleNode("bookstore");
    XmlNodeList nodeList = root.ChildNodes;
    foreach (XmlNode node in nodeList)
    {
        XmlElement ele = (XmlElement)node;
        if (ele.ChildNodes.Count <= 0)
            //只能刪除直接子節點
            root.RemoveChild(node);
    }
    doc.Save(_Store);
}

4.查詢列表

/// <summary>
/// 顯示所有的數據
/// </summary>
public static void ShowOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);

    XmlNode root = doc.SelectSingleNode("bookstore");
    XmlNodeList nodeList = root.ChildNodes;
    foreach (var node in nodeList)
    {
        XmlElement ele = (XmlElement)node;
        Console.WriteLine(ele.GetAttribute("genre"));
        Console.WriteLine(ele.GetAttribute("ISBN"));
        XmlNodeList nodeList2 = ele.ChildNodes;
        foreach (XmlNode node2 in nodeList2)
        {
            Console.WriteLine(node2.InnerText);
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM