C#对XML文件的创建及增删改查操作

C#对XML文件的创建及增删改查操作

class config
{

   //文件的创建
    public static void createItems()
    {
        XDocument xdoc = new XDocument();
        string path = Path.Combine(Application.StartupPath,"config.xml");
        if(!File.Exists(path))//文件不存在创建文件
        {
            XElement root = new XElement("XML");
            root.SetAttributeValue("Hand","COM3");
            root.SetAttributeValue("Camera", "COM4");
            root.SetAttributeValue("Plc", "S7-1200");
            root.SetAttributeValue("localIp", "192.168.1.20");
            root.SetAttributeValue("localPort", "0");
            root.SetAttributeValue("remoteIp", "192.168.1.10");
            root.SetAttributeValue("remotePort", "102");
            root.SetAttributeValue("runMode", "测试模式");
            root.SetAttributeValue("scanMode", "静态扫码");
            xdoc.Add(root);
            xdoc.Save(path);
        }
    }
    //文件中相关属性的修改
    public static void modifyItem(string name,string value)
    {
        XDocument xdoc = new XDocument();
        string path = Path.Combine(Application.StartupPath, "config.xml");
        xdoc = XDocument.Load(path);

        try
        {
            if (Find(name))
            {
                xdoc.Root.SetAttributeValue(name, value);
                xdoc.Save(path);
            }
            else MessageBox.Show("输入的属性不存在,请重新输入!");
        }
        catch
        {
            MessageBox.Show(name + "不存在,请重新输入");
        }

    }

   //判定文件中是否存在相关属性,如果存在,不添加,不存在添加改属性
    public static bool Find(string name)
    {
        XDocument xdoc = new XDocument();
        string path = Path.Combine(Application.StartupPath, "config.xml");
        xdoc = XDocument.Load(path);

        try
        {
            IEnumerable<XAttribute> list = xdoc.Root.Attributes();
            foreach (XAttribute e in list)
            {
                if (e.Name == name) return true;
            }
            return false;
        }
        catch
        {
            MessageBox.Show(name + "不存在,请重新输入");
            return false;
        }

    }
    //在文件中查找改属性
    public static string FindItems(string name)
    {
        XDocument xdoc = new XDocument();
        string path = Path.Combine(Application.StartupPath, "config.xml");
        xdoc = XDocument.Load(path);
        try
        {
            return xdoc.Root.Attribute(name).Value;
        }
        catch
        {
            return name + "不存在,请重新输入";
        }
    }
    //增加改属性
    public static void increaseItems(string name,string value)
    {
        XDocument xdoc = new XDocument();
        string path = Path.Combine(Application.StartupPath, "config.xml");
        xdoc = XDocument.Load(path);
        try
        {
            if (!Find(name))
            {
                xdoc.Root.SetAttributeValue(name, value);
                xdoc.Save(path);
            }
            else MessageBox.Show("输入的属性存在,该属性只能进行修改");
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        
    }

//删除改属性
public static void deleItems(string name)
{
XDocument xdoc = new XDocument();
string path = Path.Combine(Application.StartupPath, “config.xml”);
xdoc = XDocument.Load(path);
try
{
xdoc.Root.Attribute(name).Remove();
xdoc.Save(path);
}
catch
{
MessageBox.Show(“你要删除的条目不存在”);
}
}
}
Alt

带尺寸的图片: Alt

居中的图片: Alt

居中并且带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.2.0 开始 我的操作 确认? 结束 yes no
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值