首页 > 学院 > 开发设计 > 正文

[C#]对于XML操作代码封装

2019-11-17 03:18:32
字体:
来源:转载
供稿:网友

[C#]对于xml操作代码封装

对于XML读写操作,项目中经常要用到,之前木有好好总结过,例如LINQ TO XML也用过,这次无意发现XPATH对于XML的查询极为方便,索性把XML的操作总结以便后续方便使用。

1. 新增XML文件

                XMLToolV2 _xmlHelper = new XMLToolV2(@"C:/20140311blogs.xml");//xml保存路径或者读取路径                _xmlHelper.Create("Person", "utf-8");//跟节点名称:person;encode:utf-8                XmlElement _person = _xmlHelper.CreateElec("Name", "Yan-Zhiwei");//在跟节点后创建person节点                _xmlHelper.SetAttribute(_person, "Gender", "Man");//设置person节点属性Gender                _xmlHelper.SetAttribute(_person, "Address", "shanghai");//设置person节点属性Address                _xmlHelper.Save();//保存xml文件

上述代码实现效果:

image

那在Person节点继续增加节点,实现也很简单

                _xmlHelper.Create("Person", "utf-8");//跟节点名称:person;encode:utf-8                XmlElement _person = _xmlHelper.CreateElec("Name", "Yan-Zhiwei");//在跟节点后创建person节点                _xmlHelper.SetAttribute(_person, "Gender", "Man");//设置person节点属性Gender                _xmlHelper.SetAttribute(_person, "Address", "shanghai");//设置person节点属性Address                XmlElement _workLh = _xmlHelper.CreateElec(_person, "Work", "shanghai LH");//在person节点下增加work节点                _xmlHelper.SetAttribute(_workLh, "Year", "2013~");//设置work节点属性Title                XmlElement _workRK = _xmlHelper.CreateElec(_person, "Work", "shanghai Ranking");//在person节点下增加work节点                _xmlHelper.SetAttribute(_workRK, "Year", "2011~2013");//设置work节点属性Title                _xmlHelper.Save();//保存xml文件

上述代码实现效果:

image

 

2.读取节点的值,个人感觉xpath方式比linq to xml更为明了方便

                XMLToolV2 _xmlHelper = new XMLToolV2(@"C:/20140311blogs.xml");//xml保存路径或者读取路径                XmlNode _person = _xmlHelper.Select("Person/Name/text()");                Console.WriteLine("Name:" + _person.InnerText);                XmlNode _gender = _xmlHelper.Select("Person/Name[@Gender='Man']");                Console.WriteLine("Gender:" + _gender.Attributes["Gender"].Value);                Console.WriteLine("Address:" + _gender.Attributes["Address"].Value);                Console.WriteLine("-----------------------------");                XmlNodeList _workRecord = _xmlHelper.SelectNodeList("Person/Name/Work");                XMLToolV2.Loop(_workRecord, (XmlNode node) =>                {                    Console.WriteLine("Work:" + node.InnerText + " Year:" + node.Attributes["Year"].Value);                });

上述代码实现效果:

image

对于xpath语法,更多请参考:http://www.vevb.com.cn/xquery/index.asp

 

3.格式化显示XML

                XMLToolV2 _xmlHelper = new XMLToolV2(@"C:/20140311blogs.xml");//xml保存路径或者读取路径                string _xmlString = _xmlHelper.ShowXml();                Console.WriteLine(XMLToolV2.FormatXml(_xmlString, "utf-8"));
上述代码实现效果:

image

 

4.XMLToolV2源代码

    public class XMLToolV2    {        static string _xmlPath;        static XmlDocument _xmlDoc { get; set; }        static XmlElement _xmlRoot;        public XMLToolV2(string xmlPath)        {            _xmlPath = xmlPath;            LoadXmlDoc()
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表