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
pigyun怎么样?PIGYunData成立于2019年,2021是PIGYun为用户提供稳定服务的第三年,目前商家提供香港CN2线路、韩国cn2线路、美西CUVIP-9929、GIA等线路优质VPS,基于KVM虚拟架构,商家采用魔方云平台,所有的配置都可以弹性选择,目前商家推出了七月优惠,韩国和美国所有线路都有相应的促销,六折至八折,性价比不错。点击进入:PIGYun官方网站地址PIGYUN优惠...
inlicloud怎么样?inlicloud(引力主机)主要产品为国内NAT系列VPS,目前主要有:上海联通NAT(200Mbps带宽)、宿州联通NAT(200Mbps带宽)、广州移动NAT(200Mbps带宽)。根据官方的说法国内的NAT系列VPS不要求备案、不要求实名、对中转要求也不严格,但是,禁止任何形式的回国!安徽nat/上海联通/广州移动/江门移动nat云主机,2核1G/200Mbps仅...
农历春节将至,腾讯云开启了热门爆款云产品首单特惠秒杀活动,上海/北京/广州1核2G云服务器首年仅38元起,上架了新的首单优惠活动,每天三场秒杀,长期有效,其中轻量应用服务器2G内存5M带宽仅需年费38元起,其他产品比如CDN流量包、短信包、MySQL、直播流量包、标准存储等等产品也参与活动,腾讯云官网已注册且完成实名认证的国内站用户均可参与。活动页面:https://cloud.tencent.c...