【C# MVC工具类】DataSet/DataTable 与Xml文件的互相转化

来源:互联网 发布:防抖插件mac 编辑:程序博客网 时间:2024/05/29 20:01

无论做接口还是出于某种目的缓解服务器压力的时候,我们喜欢用xml文件去做,这样简化了很多操作。但是在C#中如何操作呢?

一:DataSet/Datatable读取xml文件。
这个很简单了,C#直接提供了api,我们直接调用就可以了:

DataSet ds = new DataSet();ds.ReadXml(filePath + fileName);

当然我们要的可能不是DataSet或是DataTable,这样就需要我们自己去解析xml文件,xml文件无非就是Node和Attribute两个属性。

二:根据DataSet/DataTable生成xml文件。
这里重点讲一下生成xml,当然你知道DataSet如何生成xml,自然也就知道List如何生成xml,都是一样的道理。
1). 使用linq to xml生成xml文件:

     using System.Xml.Linq;            private void button1_Click(object sender, EventArgs e)       {           XNamespace ns = "http://www.xxx.com/XxxSystem";           XDocument doc = new XDocument(               new XDeclaration("1.0","UTF-8",null),               new XElement(ns + "Persons",                   new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),                                             new XElement("Person",                       new XAttribute("id","1"),                       new XElement("Name","xx"),                       new XElement("Age",xx)                   ),                   new XElement("Person",                       new XAttribute("id", "2"),                       new XElement("Name", "xx"),                       new XElement("Age", xx)                   )               )            );           doc.Save("person.xml");       } 

2.)使用linq to xml 查询xml数据:

using(StreamReader sr = new StreamReader("person.xml",Encoding.UTF8)){    XDocument doc = XDocument.Load(sr);    XNameSpace ns = "heep:/www.xxxxx.cn.com/xxxx";    var query = doc.Descendants(ns+"Psersons")        .Elements(ns+"Person")        .Where(p => p.Attribute("id").Value == "1")        .Select(p => new         {            Name = p.Element(ns+"Name").Value,            Age = p.Element(ns+"Age").Value        });        foreach(var p in query)        {            Show(string.Format("{0}:{1}",p.Name,p.Age))        }}

3).使用XmlDocument生成xml文件:
这里使用的list作例子,没有使用DataSet。

XmlDocument doc = new XmlDocument();            doc.LoadXml("<UserMenu></UserMenu>");            var parentlist = list.Where(p => p.Parent_Menu_List_Id.ToString() == "").OrderBy(p => p.Serial).ToList();            for (int i = 0; i < parentlist.Count(); i++)            {                XmlElement elment = doc.CreateElement("HderMenu");                XmlAttribute hMenuName = doc.CreateAttribute("MenuName");                hMenuName.Value = parentlist[i].Request_Name;                elment.Attributes.Append(hMenuName);                XmlAttribute hMenuUrl = doc.CreateAttribute("MenuUrl");                hMenuUrl.Value = parentlist[i].Request_URL;                elment.Attributes.Append(hMenuUrl);                XmlAttribute hMenuIcon = doc.CreateAttribute("MenuIcon");                hMenuIcon.Value = parentlist[i].Icon_Name;                elment.Attributes.Append(hMenuIcon);                var childlist = list.Where(p => p.Parent_Menu_List_Id == parentlist[i].Menu_List_Id).OrderBy(p => p.Serial).ToList();                for (int j = 0; j < childlist.Count(); j++)                {                    XmlElement subelment = doc.CreateElement("SubMenu");                    XmlAttribute subMenuName = doc.CreateAttribute("MenuName");                    subMenuName.Value = childlist[j].Request_Name;                    subelment.Attributes.Append(subMenuName);                    XmlAttribute subMenuUrl = doc.CreateAttribute("MenuUrl");                    subMenuUrl.Value = childlist[j].Request_URL;                    subelment.Attributes.Append(subMenuUrl);                    XmlAttribute subMenuIcon = doc.CreateAttribute("MenuIcon");                    subMenuIcon.Value = childlist[j].Icon_Name;                    subelment.Attributes.Append(subMenuIcon);                    elment.AppendChild(subelment);                }                doc.DocumentElement.AppendChild(elment);            }            doc.Save(path);
0 0