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

    }
    }

  • 相关阅读:
    [转]读懂概率图模型:从基本概念和参数估计开始
    [转]ResNet讲解
    [转] GPT-2通俗详解
    Linux/IO基础
    Https:深入浅出HTTPS的交互过程
    Poll/Select/Epoll
    IO相关知识
    认真分析mmap:是什么 为什么 怎么用【转】
    页面滚动时导航渐变
    SSH Secure Shell Client的傻瓜式使用方法
  • 原文地址:https://www.cnblogs.com/aqing0/p/11460390.html
Copyright © 2011-2022 走看看