linq to xml操作XML的c#代码
LINQ to XML提供了更方便的读写xml方式。前几篇文章的评论中总有朋友提你为啥不用linq to xml现在到时候了 linq to xml出场了。 .Net中的System.Xml.Linq命名空间提供了linq to xml的支持。这个命名空间中的XDoc umentXElem ent以及XTextXAttribute提供了读写xml文档的关键方法。
1.使用linq to xml写xml
使用XD o c ument的构造函数可以构造一个Xml文档对象使用XElement对象可以构造一个xml节点元素使用XAttribute构造函数可以构造元素的属性使用XText构造函数可以构造节点内的文本。 如下实例代码class Programstatic void Main(string[] args)var xDoc=new XDocument(new XElement(“root”,new XElement(“dog”,new XText(“dog said black is a beautify color”),new XAttribute(“c olor”, “black”)),new XElement(“c at”),new XElement(“p ig”, “pig is great”)));
//xDoc输出xml的enc oding是系统默认编码对于简体中文操作系统是gb2312
//默认是缩进格式化的xml而无须格式化设置xDoc.Save(Console.Out);
Cons ole.Read();
}
上面代码将输出如下Xml
?xml version=“1.0”enc oding=“gb2312”?
rootdog color=“black”dog said black is a beautify color/dogcat/pig pig is great /pig
/root
可以看出linq to xml比XmlDocument和XmlWriter要方便很多。
2.使用linq to xml读取xml
Linq是从集合中查询对象 在linq to xml 中的集合是通过XElement 的Elements(),Elements(string name),以及Descendants 、 Desc endantsAndSelf、 Anc estors 、Anc estorsAndS elf的几个重载方法中获得。 获得XElement集合之后可以通过XElement的Attribute(string name)方法获得元素的属性值可以通过XElement的Value属性获得节点的文本值使用linq就可以方便的做查询做筛选排序了还是上例中的xml我们要读取root的所有字节点并打印出来如下代码class Programstatic void Main(string[] args)var xDoc=new XDocument(new XElement(“root”,new XElement(“dog”,new XText(“dog said black is a beautify color”),new XAttribute(“c olor”, “blac k”)),new XElement(“c at”),new XElement(“p ig”, “pig is great”)));
//xDoc输出xml的enc oding是系统默认编码对于简体中文操作系统是gb2312
//默认是缩进格式化的xml而无须格式化设置
xDoc.Save(Console.Out);
Console.WriteLine();var query=from item in xDoc.Element(“root”).Elements()select new
Typ eNam e=item.Nam e,
Saying=item.Value,
Color=item.Attribute(“c olor”)==null?(s tring)null:item.Attribute(“c olor”).Va lueforeach(var item in query)
Console.WriteLine(“{0} „s color is {1},{0} said{2}”,item.TypeName,item.Color??”Unknown”,item.Saying??”nothing”);
Cons ole.Read();
}
3.Linq to xml简单的应用
应用需求 读取博客园的rss然后在页面上输出最新的10篇博客信息
实现要点 通过XD oc ument的Lo ad静态方法载入Xml通过linq查询最新10条数据
代码如下:
%@P age Language=“C#”Auto Event Wireup=“true”%script runat=“s erver”protected override void OnLoad(EventArgs e)
//实际应用,通过读取博客园的RS S生成Html代码显示最新的博客列表
//使用XD oc ument的Lo ad静态方法载入Xml
var r ss XD o c=XD oc ument.Lo ad(“http://www.c nb lo g s.c om/r s s”);
//使用linq to xml查询前10条新博客var queryBlogs=(from blog in rssXDoc.Descendants(“item”)select new
Title=blog.Element(“title”).Value,
Url=blog.E lement(“link”).Va lue,
PostTime=DateTime.Parse(blog.Element(“pubDate”).Value)
}).Take(20);repeaterBlogs.DataSourc e=queryBlogs;repeaterBlogs.DataBind();bas e.OnLoad(e);
/script
!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”“http://w ww.w 3.org/T R/xhtml 1/DT D/xhtml 1-trans itiona l.dtd”html xmlns=“http://www.w 3.org/1999/xhtml”head runat=“s erver”title Linq to Xml实例/title
/headbodyasp:Repeater ID=“repeater Blogs”Enab leView State=“fals e”runat=“s erver”
ItemTemplateli span
%#Eval(“PostTime”)%/span a href=“%#Eval(“Url”)%“%#Eval(“Title”)%/a/li/ItemTemplate
/as p:Rep eater
/ol
/body
/html
说明一下:gcorelabs的俄罗斯远东机房“伯力”既有“Virtual servers”也有“CLOUD SERVICES”,前者是VPS,后者是云服务器,不是一回事;由于平日大家习惯把VPS和云服务器当做一回事儿,所以这里要特别说明一下。本次测评的是gcorelabs的cloud,也就是云服务器。 官方网站:https://gcorelabs.com 支持:数字加密货币、信用卡、PayPal...
酷锐云是一家2019年开业的国人主机商家,商家为企业运营,主要销售主VPS服务器,提供挂机宝和云服务器,机房有美国CERA、中国香港安畅和电信,CERA为CN2 GIA线路,提供单机10G+天机盾防御,提供美国原生IP,支持媒体流解锁,商家的套餐价格非常美丽,CERA机房月付20元起,香港安畅机房10M带宽月付25元,有需要的朋友可以入手试试。酷锐云自开业以来一直有着良好的产品稳定性及服务态度,支...
Virmach 商家算是比较久且一直在低价便宜VPS方案中玩的不亦乐乎的商家,有很多同时期的商家纷纷关闭转让,也有的转型到中高端用户。而前一段时间也有分享过一次Virmach商家推出所谓的一次性便宜VPS主机,比如很低的价格半年时间,时间到服务器也就关闭。这不今天又看到商家有提供这样的产品。这次的活动产品包括圣何塞和水牛城两个机房,为期六个月,一次性付费用完将会取消,就这么特别的产品,适合短期玩玩...