• C# 本地xml文件进行增删改查


    项目添加XML文件:FaceXml.xml,并复制到输出目录

    FaceXml.xml

    <?xml version="1.0" encoding="utf-8"?>

    <faces>
    <face>
    <faceid></faceid>
    <facebyte>/facebyte>
    </face>
    </faces>

    项目添加XmlHelper帮助类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;

    namespace AvoidMisplace
    {
    public class XmlHelper
    {
    //项目输出目录的FaceXml.xl文件位置
    public static string facepath = AppDomain.CurrentDomain.BaseDirectory + "FaceXml.xml";

    //查询是否存在faceid值为num的节点
    public static bool QueryFaceXml(string num)
    {
    try
    {
    XDocument xml = XDocument.Load(facepath);
    XElement face = (from item in xml.Element("faces").Elements()
    where item.Element("faceid").Value == num
    select item).SingleOrDefault();
    if (face != null)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    catch (Exception ex)
    {
    return false;
    }
    }

    //获取所有faces节点下facebyte值
    public static List<string> GetFaceXml()
    {
    try
    {
    XDocument xml = XDocument.Load(facepath);
    var query = (from item in xml.Element("faces").Elements()
    select item.Element("faceid").Value).ToList();
    return query;
    }
    catch (Exception)
    {
    return null;
    }
    }

    //查询faceid值为num的节点对应facebyte值,
    public static byte[] ReadFaceXml(string num)
    {
    try
    {
    XDocument xml = XDocument.Load(facepath);
    var query = (from item in xml.Element("faces").Elements()
    where item.Element("faceid").Value == num
    select item).SingleOrDefault();
    return query.Element("facebyte").Value;
    }
    catch (Exception ex)
    {
    return null;
    }
    }

    //新增一个face节点写入键值对
    public static bool WriteFaceXml(string num, byte[] array)
    {
    try
    {
    XDocument xml = XDocument.Load(facepath);
    XElement face = new XElement("face", new XElement("faceid", num), new XElement("facebyte", array));
    xml.Element("faces").Add(face);
    xml.Save(facepath);
    //LogHelper.Debug("xml添加:" + num + ",成功");
    return true;
    }
    catch (Exception ex)
    {
    //LogHelper.Error("xml添加:" + num + "," + ex.Message);
    return false;
    }
    }

    //删除faceid值为num的节点
    public static bool DelFaceXml(string num)
    {
    try
    {
    XDocument xml = XDocument.Load(facepath);
    XElement face = (from item in xml.Element("faces").Elements()
    where item.Element("faceid").Value == num
    select item).SingleOrDefault();
    if (face != null)
    {
    face.Remove();
    xml.Save(facepath);
    //LogHelper.Debug("xml删除:" + num + ",成功");
    return true;
    }
    else
    {
    //LogHelper.Debug("xml无:" + num);
    return false;
    }
    }
    catch (Exception ex)
    {
    //LogHelper.Error("xml删除:" + num + "," + ex.Message);
    return false;
    }
    }

    }
    }

  • 相关阅读:
    Openrasp源码分析
    feifeicms后台任意文件读取
    python之迭代器和生成器
    java之导入excel
    jquery单击事件的写法
    java之高效操作文件
    多条件搜索优化sql
    java之代码复用
    java之接口文档规范
    eclipse之常用快捷键
  • 原文地址:https://www.cnblogs.com/aqing0/p/11460390.html
Copyright © 2020-2023  润新知