博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 操作XML
阅读量:6242 次
发布时间:2019-06-22

本文共 5222 字,大约阅读时间需要 17 分钟。

public class KeyValue    {        private string key = string.Empty;        public string Key        {            get { return key; }            set { key = value; }        }        private string value = string.Empty;        public string Value        {            get { return this.value; }            set { this.value = value; }        }        public KeyValue(string key, string value)        {            this.Key = key;            this.Value = value;        }    }    public class Result    {        public static string CreateResult(string status, string message, List
rsList) { try { XmlDocument xmlDoc = new XmlDocument(); //创建类型声明节点 XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", ""); xmlDoc.AppendChild(node); //创建根节点 XmlNode root = xmlDoc.CreateElement("data"); xmlDoc.AppendChild(root); XmlNode result = xmlDoc.CreateNode(XmlNodeType.Element, "result", null); CreateNode(xmlDoc, result, "status", status); CreateNode(xmlDoc, result, "message", message); root.AppendChild(result); XmlNode writetext = xmlDoc.CreateNode(XmlNodeType.Element, "writetext", null); root.AppendChild(writetext); if (rsList != null) { for (int i = 0; i < rsList.Count; i++) { CreateNode(xmlDoc, writetext, rsList[i].Key, rsList[i].Value); } } return xmlDoc.InnerXml; } catch (Exception) { throw; } } public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value) { XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentNode.AppendChild(node); } public static XmlDocument CreateTitle(string status, string message) { try { XmlDocument xmlDoc = new XmlDocument(); //创建类型声明节点 XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", ""); xmlDoc.AppendChild(node); //创建根节点 XmlNode root = xmlDoc.CreateElement("data"); xmlDoc.AppendChild(root); XmlNode result = xmlDoc.CreateNode(XmlNodeType.Element, "result", null); CreateNode(xmlDoc, result, "status", status); CreateNode(xmlDoc, result, "message", message); root.AppendChild(result); return xmlDoc; } catch (Exception) { throw; } } public static string CreateList(XmlDocument xmlDoc, string elementParent, string item, List
rsList) { try { XmlNode xmlNode = xmlDoc.SelectSingleNode("data"); XmlNode writetext = xmlNode.SelectSingleNode(elementParent); XmlNode itemNode = null; if (writetext == null) { writetext = xmlDoc.CreateNode(XmlNodeType.Element, elementParent, null); xmlNode.AppendChild(writetext); itemNode = xmlDoc.CreateNode(XmlNodeType.Element, item, null); writetext.AppendChild(itemNode); } else { itemNode = xmlDoc.CreateNode(XmlNodeType.Element, item, null); writetext.AppendChild(itemNode); } if (rsList != null) { for (int i = 0; i < rsList.Count; i++) { CreateNode(xmlDoc, itemNode, rsList[i].Key, rsList[i].Value); } } return xmlDoc.InnerXml; } catch (Exception) { throw; } } } public enum ResultState { success = 1, failed = 0, none = -1, error = -2, mserror = -3 } public class HelperXml { ///
/// 解析xml /// ///
public static XmlNode AnalysisXml(string xmlfile) { try { System.Xml.XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlfile); XmlNode root = xmlDoc.SelectSingleNode("master"); //获取根节点下所有子节点 XmlNodeList nodelist = root.ChildNodes; return root; } catch (Exception) { throw; } } ///
/// 获取指定节点下所有节点 /// ///
///
///
public static XmlNode GetAppointXmlnode(XmlNode xmlNode, string nodeName) { try { XmlNode node = xmlNode.SelectSingleNode(nodeName); return node; } catch (Exception) { throw; } } }

 

转载地址:http://plsia.baihongyu.com/

你可能感兴趣的文章
Spring【DAO模块】就是这么简单
查看>>
wamp虚拟主机配置
查看>>
深入Spring Boot:ClassLoader的继承关系和影响
查看>>
Android带有删除按钮的EditText:EditTextWithDeleteButton
查看>>
2:C#TPL探秘
查看>>
Android Segmented RadioButton
查看>>
Java中菜单组件
查看>>
git reset revert 回退回滚取消提交返回上一版本
查看>>
适配mpvue平台的的微信小程序日历组件mpvue-calendar
查看>>
Consul Config 使用Git做版本控制的实现
查看>>
我们必须要知道的RESTful服务最佳实践
查看>>
百度调整Q2营收预期
查看>>
阿里巴巴智慧建筑(IB)峰会 与筑梦者共建新生态
查看>>
Apache Zeppelin安装及使用
查看>>
Redis实现微博后台业务逻辑系列(四)
查看>>
Power5连接使用DS8000遇到问题处理一例
查看>>
迈克菲实验室:Flame病毒的深度分析
查看>>
用十条命令在一分钟内检查Linux服务器性能[转]
查看>>
深入理解bash及字符串的处理
查看>>
Python异步IO --- 轻松管理10k+并发连接
查看>>