• 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;
    }
    }

    }
    }

  • 相关阅读:
    【HDOJ】4982 Goffi and Squary Partition
    【HDOJ】4983 Goffi and GCD
    【算法导论】学习笔记——第7章 快速排序
    【算法导论】学习笔记——第6章 堆排序
    【HDOJ】4956 Poor Hanamichi
    【HDOJ】2492 Ping pong
    【Linux】鸟哥的Linux私房菜基础学习篇整理(十二)
    【Linux】鸟哥的Linux私房菜基础学习篇整理(十一)
    统计硬币
    放大的X
  • 原文地址:https://www.cnblogs.com/aqing0/p/11460390.html
Copyright © 2020-2023  润新知