c#读取xml文档

本文提供了一个使用C#创建、获取、修改、添加和删除XML文件信息的示例。通过控制台应用,用户可以进行一系列对XML文件的操作,包括创建新文件、读取文件内容、修改信息、添加节点以及删除节点。每个操作都通过详细的代码实现,展示了如何利用.NET Framework的XML功能来处理XML文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. [代码][C#]代码     
\
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
 
namespace XmlFileTest
{
    class Program
    {
        static void Main(string[] args)
        {
             bool isCreateXml=false;
            string xmlFilePath = @"C:\Users\Administrator\Desktop\myXml.xml";
            while (true)
            {
                Console.WriteLine("输入1:创建xml\n输入2:获取xml信息\n输入3:修改xml\n输入4:添加节点\n输入5:删除节点\n输入6:退出");
                string flag = Console.ReadLine();
                switch (flag)
                {
                    case "1":
                            CreateXml(xmlFilePath);
                            isCreateXml = true;
                        break;
                    case "2":
                        if (isCreateXml)
                        {
                            GetXmlInformation(xmlFilePath);
                        }
                        else { Console.WriteLine("xml文件尚未创建完成"); }
                        break;
                    case "3":
                        if (isCreateXml)
                        {
                            ModifyXmlInformation(xmlFilePath);
                            Console.WriteLine("xml修改完成,输入2查看\n");
                        }
                        else { Console.WriteLine("xml文件尚未创建完成"); }
                        break;
                    case "4":
                        if (isCreateXml)
                        {
                            AddXmlInformation(xmlFilePath);
                            Console.WriteLine("xml节点添加完成,输入2查看\n");
                        }
                        else { Console.WriteLine("xml文件尚未创建完成"); }
                        break;
                    case "5":
                        if (isCreateXml)
                        {
                            DeleteXmlInformation(xmlFilePath);
                            Console.WriteLine("xml节点删除完成,输入2查看\n");
                        }
                        else { Console.WriteLine("xml文件尚未创建完成"); }
                        break;
                    case "6":
                        return;
                    default:
                        break;
                }
            }
        }
      public static   void CreateXml(string xmlPath)
        {
            XmlDocument myXml = new XmlDocument();
            XmlElement root = myXml.CreateElement("异界小说");
            myXml.AppendChild(root);
            //根节点
 
            XmlElement bookOne = myXml.CreateElement("琥珀之剑");
            bookOne.SetAttribute("作者", "绯炎");
            bookOne.SetAttribute("简介", "命运在我眼前分开成两条互不相关笔直的线,");
 
            root.AppendChild(bookOne);
            //第一层
            XmlElement bookChapter = myXml.CreateElement("章节");
            bookChapter.SetAttributeNode("章节数", "二百二十六");
            bookOne.AppendChild(bookChapter);
            //第二章
            XmlElement bookContext1 = myXml.CreateElement("内容");
            bookContext1.SetAttribute("第一章", "梦中人");
            bookContext1.InnerText = "http://read.qidian.com/BookReader/1784765,30184328.aspx";
            bookChapter.AppendChild(bookContext1);
           
          XmlElement bookContext2 = myXml.CreateElement("内容");
            bookContext2.SetAttribute("第二章", "苏菲的世界");
            bookContext2.InnerText = "http://read.qidian.com/BookReader/1784765,30194232.aspx";
            bookChapter.AppendChild(bookContext2);
 
            XmlElement bookContext3 = myXml.CreateElement("内容");
            bookContext3.SetAttribute("第三章", "亡灵");
            bookContext3.InnerText = "http://read.qidian.com/BookReader/1784765,30197921.aspx";
            bookChapter.AppendChild(bookContext3);
             
            XmlElement bookTwo = myXml.CreateElement("海盗系统");
            bookTwo.SetAttribute("作者", "正气蛋");
            bookTwo.SetAttribute("简介", @"在惊涛怒浪中扬帆远航, 在炮火纷飞中饮酒大笑, 这是海盗的赞歌! ");
 
            root.AppendChild(bookTwo);
            XmlElement bookChapter2 = myXml.CreateElement("章节");
            bookChapter2.SetAttributeNode("章节数", "五百五十一");
            bookTwo.AppendChild(bookChapter2);
 
            XmlElement bookContext4 = myXml.CreateElement("内容");
            bookContext4.SetAttribute("第一章", "海盗王");
            bookContext4.InnerText = "http://read.qidian.com/BookReader/2123512,34819027.aspx";
            bookChapter2.AppendChild(bookContext4);
 
            XmlElement bookContext5 = myXml.CreateElement("内容");
            bookContext5.SetAttribute("第二章", "孤岛");
            bookContext5.InnerText = "http://read.qidian.com/BookReader/2123512,34824359.aspx";
            bookChapter2.AppendChild(bookContext5);
 
            XmlElement bookContext6 = myXml.CreateElement("内容");
            bookContext6.SetAttribute("第三章", "残图");
            bookContext6.InnerText = "http://read.qidian.com/BookReader/2123512,34833260.aspx";
            bookChapter2.AppendChild(bookContext6);
 
            try
            {
                myXml.Save(xmlPath);
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); }
           
        }
      public static void GetXmlInformation(string xmlPath)
      {
          try
          {
              XmlDocument myXmlFile = new XmlDocument();
              myXmlFile.Load(xmlPath);
              XmlNode root = myXmlFile.SelectSingleNode("异界小说");
              string innerXml = root.InnerXml.ToString();
              string outXml = root.OuterXml.ToString();
              Console.WriteLine(innerXml+'\n');//带根节点输出
              Console.WriteLine(outXml + '\n');//不带根节点输出
 
              XmlNodeList nodeList = root.ChildNodes;
              foreach (XmlNode xmlNode in nodeList)
              {
                  XmlAttributeCollection xmlAttrColl = xmlNode.Attributes;
                  foreach (XmlAttribute xmlAttr in xmlAttrColl)
                  {
                      string name = xmlAttr.Name;
                      string value = xmlAttr.Value;
                      Console.WriteLine("{0} = {1}", name, value);
 
                  }
                  if (xmlNode.HasChildNodes)
                  {
                      XmlNode secNode = xmlNode.FirstChild;
                      XmlAttributeCollection secXmlAttrColl = secNode.Attributes;
                      foreach (XmlAttribute xmlAttr in secXmlAttrColl)
                      {
                          string name = xmlAttr.Name;
                          string value = xmlAttr.Value;
                          Console.WriteLine("{0} = {1}", name, value);
                      }
                      if (secNode.HasChildNodes)
                      {
                          foreach (XmlNode node in secNode)
                          {
                              XmlNode thirdNode = node;
                              XmlAttributeCollection thirdXmlAttrColl = thirdNode.Attributes;
                              foreach (XmlAttribute xmlAttr in thirdXmlAttrColl)
                              {
                                  string name = xmlAttr.Name;
                                  string value = xmlAttr.Value;
                                  Console.WriteLine("{0} = {1}", name, value);
                              }
                              string innerText = node.InnerText;
                              Console.WriteLine(innerText);
                          }
                      }
                  }
              }
              //逐条输出
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
      public static void ModifyXmlInformation(string xmlPath)
      {
          try
          {
              XmlDocument myXmlFile = new XmlDocument();
              myXmlFile.Load(xmlPath);
              XmlNode root = myXmlFile.FirstChild;
              XmlNodeList nodeList = root.ChildNodes;
              foreach (XmlNode firNode in nodeList)
              {
                  if (firNode.Attributes["简介"].Value.Equals("命运在我眼前分开成两条互不相关笔直的线,"))
                  {
                      firNode.Attributes["简介"].Value = "已经成功改变了值";
                  }
                  if (firNode.Attributes["简介"].Value.Equals(@"在惊涛怒浪中扬帆远航, 在炮火纷飞中饮酒大笑, 这是海盗的赞歌! "))
                  {
                      firNode.Attributes["简介"].Value = "已经成功改变了值";
                  }
              }
              myXmlFile.Save(xmlPath);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
      public static void DeleteXmlInformation(string xmlPath)
      {
          try
          {
              XmlDocument myXmlFile = new XmlDocument();
              myXmlFile.Load(xmlPath);
              XmlNode root = myXmlFile.FirstChild;
              XmlNodeList nodeList = root.ChildNodes;
              foreach (XmlNode firNode in nodeList)
              {
                  if (firNode.HasChildNodes)
                  {
                      foreach (XmlNode secNode in firNode)
                      {
                          XmlNode lastNode = secNode.LastChild;
                          lastNode.RemoveAll();
                          secNode.RemoveChild(lastNode);
                      }
                  }
              }
              myXmlFile.Save(xmlPath);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
      public static void AddXmlInformation(string xmlPath)
      {
          try
          {
              XmlDocument myXmlFile = new XmlDocument();
              myXmlFile.Load(xmlPath);
              XmlNode root = myXmlFile.FirstChild;
              XmlNodeList nodeList = root.ChildNodes;
              foreach (XmlNode firNode in nodeList)
              {
                  if (firNode.HasChildNodes)
                  {
                      foreach (XmlNode secNode in firNode)
                      {
                          XmlElement newElement = myXmlFile.CreateElement("长文章");
                          newElement.SetAttribute("字数", "100w以上");
                          secNode.AppendChild(newElement);
                      }
                  }
              }
              myXmlFile.Save(xmlPath);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值