一篇文章搞懂C# .Net对Xml文件进行增、删、改、查操作

201 阅读1分钟

一:准备后自己的Xml文件

image.png

二:使用XmlDocument文件对象获取自己需要的xml的Node节点数据

方法如下:

     /// <summary>
    ///  读取xml文件数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public IActionResult Get()
    {
        string path = "D:\\Test\\ReadJson-.Net6.0\\ReadJson-.Net6.0\\MyXml.xml";
        //实例化xml文件对象
        XmlDocument xmlDocument = new XmlDocument();
        //加载xml文件
        xmlDocument.Load(path);

        //获取xml文件的根节点
        XmlNode firstNode = xmlDocument.DocumentElement;

        //假设读取跟节点中的第一个子节点的数据
        return Ok(new
        {
            Id = firstNode.ChildNodes[0].ChildNodes[0].InnerText,
            Name = firstNode.ChildNodes[0].ChildNodes[1].InnerText,
        });
    }

    /// <summary>
    /// 修改xml文件数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("update")]
    public IActionResult Update()
    {
        string id = Guid.NewGuid().ToString();
        string name = "影流之主";
        string path = "D:\\Test\\ReadJson-.Net6.0\\ReadJson-.Net6.0\\MyXml.xml";
        //实例化xml文件对象
        XmlDocument xmlDocument = new XmlDocument();
        //加载xml文件
        xmlDocument.Load(path);

        //获取xml文件的根节点
        XmlNode firstNode = xmlDocument.DocumentElement;

        firstNode.ChildNodes[0].ChildNodes[0].InnerText = id;
        firstNode.ChildNodes[0].ChildNodes[1].InnerText = name;

        //保存xml文件数据
        xmlDocument.Save(path);

        return Ok();
    }

    /// <summary>
    /// 向xml文件插入数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("add")]
    public IActionResult Add()
    {
        string id = Guid.NewGuid().ToString();
        string name = "剑魔";
        string path = "D:\\Test\\ReadJson-.Net6.0\\ReadJson-.Net6.0\\MyXml.xml";
        //实例化xml文件对象
        XmlDocument xmlDocument = new XmlDocument();
        //加载xml文件
        xmlDocument.Load(path);

        //获取xml文件的根节点
        XmlNode firstNode = xmlDocument.DocumentElement;

        //复制最后一个大子节点,作为要添加的新节点
        XmlNode newNode = firstNode.LastChild.CloneNode(true);

        newNode.ChildNodes[0].ChildNodes[0].InnerText = id;
        newNode.ChildNodes[1].ChildNodes[0].InnerText = name;


        //把新创建的节点 追加到根节点中
        firstNode.AppendChild(newNode);

        //保存xml文件数据
        xmlDocument.Save(path);

        return Ok();
    }

    /// <summary>
    /// 删除xml文件数据
    /// </summary>
    /// <returns></returns>
    [HttpGet("Deldete")]
    public IActionResult Deldete()
    {
        string id = Guid.NewGuid().ToString();
        string name = "剑魔";
        string path = "D:\\Test\\ReadJson-.Net6.0\\ReadJson-.Net6.0\\MyXml.xml";
        //实例化xml文件对象
        XmlDocument xmlDocument = new XmlDocument();
        //加载xml文件
        xmlDocument.Load(path);

        //获取xml文件的根节点
        XmlNode firstNode = xmlDocument.DocumentElement;

        //删除id为3的英雄
        if (firstNode.ChildNodes[2].ChildNodes[0].InnerText == "3")
        {
            firstNode.RemoveChild(firstNode.ChildNodes[2]);
        }

        //保存xml文件数据
        xmlDocument.Save(path);

        return Ok();
    }

如下图:为增删改查操作之后剩余的数据,显然很是成功。 image.png 以上就是我对xml文件了解的简单的读取方法,大家有什么想法,欢迎到评论区讨论吖!!!