C# 程序xml文件生成和读写操作

public class XmlFile{private string fileFolder;private string fileName;private string fPathBase = Directory.GetCurrentDirectory();  //结尾没有反斜杠public TimeConfig(string aFileFolder = "xmlConfig", string aFileName = "config"){fileFolder = aFileFolder;fileName = aFileName;}/// <summary>/// 创建xml文件/// </summary>/// <param name="aRoot"></param>/// <param name="aKey"></param>/// <param name="aValue"></param>public void CreateXmlFile(string aRoot, string aKey = "", string aValue = ""){XmlDocument xmlDoc = new XmlDocument();//创建类型声明节点  XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");xmlDoc.AppendChild(node);//创建根节点  XmlNode root = xmlDoc.CreateElement(aRoot);xmlDoc.AppendChild(root);if (!string.IsNullOrEmpty(aKey) && !string.IsNullOrEmpty(aValue)){CreateNode(aKey, aValue, xmlDoc, root);}xmlDoc.Save($"{fPathBase}//{fileFolder}//{fileName}.xml");}/// <summary>    /// 创建节点    /// </summary>    /// <param name="xmldoc"></param>  xml文档  /// <param name="parentnode"></param>父节点    /// <param name="name"></param>  节点名  /// <param name="value"></param>  节点值  ///   public void CreateNode(string name, string value, XmlDocument xmlDoc, XmlNode parentNode){XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);node.InnerText = value;parentNode.AppendChild(node);}/// <summary>/// 读取xml中某个节点的值/// </summary>/// <param name="aRoot"></param>/// <param name="aKey"></param>/// <param name="aDefaultValue"></param>/// <returns></returns>public string ReadValue(string aRoot, string aKey, string aDefaultValue){try{string bFile = $"{fPathBase}//{fileFolder}//{fileName}.xml";if (!File.Exists(bFile)){string bPath = $"{fPathBase}//{fileFolder}";if (!Directory.Exists(bPath)){Directory.CreateDirectory(bPath);}CreateXmlFile(aRoot, aKey, aDefaultValue);return aDefaultValue;}else{XmlDocument xml = new XmlDocument();xml.Load(bFile);XmlNode bNode = xml.SelectSingleNode($"//{aRoot}//{aKey}");return bNode.InnerText;}}catch(Exception e){return null;}}/// <summary>/// 给xml中某个节点赋值/// </summary>/// <param name="aRoot"></param>/// <param name="aKey"></param>/// <param name="aDefaultValue"></param>public void WriteValue(string aRoot, string aKey, string aDefaultValue,ref string aExMsg){try{string bFile = $"{fPathBase}//{fileFolder}//{fileName}.xml";if (File.Exists(bFile)){string bPath = $"{fPathBase}//{fileFolder}";if (!Directory.Exists(bPath)){Directory.CreateDirectory(bPath);}CreateXmlFile(aRoot, aKey, aDefaultValue);}else{XmlDocument xml = new XmlDocument();xml.Load(bFile);XmlNode bNode = xml.SelectSingleNode($"//{aRoot}//{aKey}");bNode.InnerText = aDefaultValue;}}catch(Exception e){aExMsg = e.Message;}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442090.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)

题干&#xff1a; Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5505 Accepted Submission(s): 3976 Problem Description The Head Elder of the tropical island of Lagrishan has a proble…

vscode中怎样格式化js代码_[VSCode插件推荐] Bracket Pair Colorizer: 为代码中的括号添上一抹亮色...

在代码编写过程中&#xff0c;各种括号 {[()]} 必不可少。然而&#xff0c;随着代码量的增加&#xff0c;你有没有因为括号的嵌套太多&#xff0c;而导致代码难以阅读&#xff1f;我们来看看下面的代码&#xff0c;在第三行代码的最后部分&#xff0c;连续出现了5个右括号&…

Asp.net MVC 从ftp服务器读取文件保存到网站本地

1 FTP连接辅助类 /// <summary>/// FTP操作单元/// </summary>public class TFTPOperationUnit{/// <summary>/// ftp地址/// </summary>private string FFTPUrl;/// <summary>/// ftp用户名/// </summary>string FFTPUserName;/// <s…

多帧点云数据拼接合并_自动驾驶:Lidar 3D传感器点云数据和2D图像数据的融合标注...

自动驾驶汽车的发展已经见证了硬件传感器记录感官数据的容量和准确度的发展。传感器的数量增加了&#xff0c;新一代传感器正在记录更高的分辨率和更准确的测量结果。 在本文中&#xff0c;我们将探讨传感器融合如何在涉及环环相扣的数据标记过程中实现更高程度的自动化。所有自…

*【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)

题干&#xff1a; Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consist…

pdf转图片记录

1 winform将pdf转图片&#xff0c;有案例&#xff0c;连接为&#xff1a;https://download.csdn.net/download/mingjing941018/20216747 2 Asp.net MVC将pdf转图片 使用Nuget安装包安装Freespire.pdf&#xff0c;控制器中相关代码&#xff1a; /// <summary>/// 将本地…

【基础知识】大数据组件HBase简述

HBase是一个开源的、面向列&#xff08;Column-Oriented&#xff09;、适合存储海量非结构化数据或半结构化数据的、具备高可靠性、高性能、可灵活扩展伸缩的、支持实时数据读写的分布式存储系统。 只是面向列&#xff0c;不是列式存储 mysql vs hbase vs clickhouse HMaster …

改变定时器获取传感器频度_广东梅州梅县压力传感器*校对

广东梅州梅县压力传感器*校对看门狗寄存器不会改变或改变不大&#xff0c;如果看门狗寄存器发生了改变或改变很大&#xff0c;则说明系统陷入“死循环”.需要进行出错处理。在工业应用中&#xff0c;严重的干扰有时会破坏中断方式控制字&#xff0c;关闭中断&#xff0c;造成看…

**【POJ - 2389】 Bull Math (高精度乘法)

题干&#xff1a; Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls answers. Read in two …

nodeType的类型

1&#xff1a;元素节点   2&#xff1a;属性节点   3&#xff1a;文本节点   4&#xff1a;CDATA区段   5&#xff1a;实体应用元素   6&#xff1a;实体   7&#xff1a;表示处理指令   8&#xff1a;注释节点   9&#xff1a;最外层的Root element,包括所有其…

springboot 不响应字段为空_面试官扎心一问:Tomcat 在 SpringBoot 中是如何启动的?...

作者&#xff1a;木木匠 http://my.oschina.net/luozhou/blog/3088908前言我们知道 SpringBoot 给我们带来了一个全新的开发体验&#xff0c;我们可以直接把 web 程序达成 jar 包&#xff0c;直接启动&#xff0c;这就得益于 SpringBoot 内置了容器&#xff0c;可以直接启动&am…

【POJ - 3250 】Bad Hair Day (单调栈)

题干&#xff1a; Some of Farmer Johns N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows heads. Each cow i has a s…

a1708硬盘转接口_资讯:希捷上架新款银河Exos系列机械硬盘,15000转+SAS协议

今日最新消息&#xff0c;希捷上架一款新品希捷银河Exos系列机械硬盘。据悉这款硬盘采用了SAS协议&#xff0c;转速高达15000RPM&#xff0c;目前公布的售价600GB为1899元RMB。据官方介绍这款希捷银河Exos系列机械硬盘为2.5英寸&#xff0c;15mm的厚度&#xff0c;最高的转速可…

ACM中关于计算几何(浮点数)的精度问题

计算几何的精度问题说到底其实是浮点数的精度问题&#xff0c;但我觉得“计算几何”比“浮点数”更能吸引眼球&#xff0c;所以选了这个标题。 1.浮点数为啥会有精度问题&#xff1a; 浮点数(以C/C为准)&#xff0c;一般用的较多的是float, double。 占字节数 数值范围 十进…

微信公众号网站开发相关记录

1 如何监听微信录音是否正常开启 wx.startRecord({success: function (ret) {alert("开始录音" JSON.stringify(ret));},fail: function (err) {alert("无法录音" JSON.stringify(err));}});

【POJ - 1182】 食物链(附超详细讲解)(并查集--种类并查集经典题)

题干&#xff1a; 动物王国中有三类动物A,B,C&#xff0c;这三类动物的食物链构成了有趣的环形。A吃B&#xff0c; B吃C&#xff0c;C吃A。 现有N个动物&#xff0c;以1&#xff0d;N编号。每个动物都是A,B,C中的一种&#xff0c;但是我们并不知道它到底是哪一种。 有人用两…

腐蚀单机怎么进_暖气片堵塞是什么原因?要怎么解决呢?

你知道散热器到底为什么堵塞吗&#xff1f;散热器堵塞了怎么办&#xff1f;下面和金旗舰散热器小编一起来看看吧~一、散热器堵塞怎么办首先&#xff0c;把进回水阀先全部关闭&#xff0c;用扳手将散热器的堵头轻轻拧开。这里需要注意的是&#xff0c;堵头对应的散热器下面要放一…

layui弹出界面空白页问题

弹出界面时&#xff0c;有时会出现空白界面&#xff0c;应该如何处理&#xff1f; 1 尝试解决方式&#xff1a;在open方法的success回调方法中&#xff0c;获取当前iframe高度&#xff0c;重新赋予新的高度&#xff1b; let ifr layero.find(iframe)[0]; let bHeight ifr.s…

vspy如何在图形面板显示报文_设备实时状态监控:如何进行工业生产设备数据采集?...

设备实时状态监控&#xff1a;如何进行工业生产设备数据采集&#xff1f;数据采集(DAQ)&#xff0c;是指从传感器和其它待测设备等模拟和数字被测单元中自动采集非电量或者电量信号,送到上位机中进行分析&#xff0c;处理。慧都设备数据采集系统解决方案工业生产设备数据采集是…

【POJ - 2236】Wireless Network (并查集)

题干&#xff1a; An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers …