zoukankan      html  css  js  c++  java
  • 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;
    }
    }

    }
    }

  • 相关阅读:
    Array.from和 map的相同用法
    js复制数组的各种方式
    set集合转数组的两种方法
    关于set集合去重的问题
    Object.is判断两个值是否相等
    数组排序
    函数默认值的取值
    标准索引
    pgsql update left join
    linux pgsql
  • 原文地址:https://www.cnblogs.com/aqing0/p/11460390.html
Copyright © 2011-2022 走看看