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
2022年春节假期陆续结束,根据惯例在春节之后各大云服务商会继续开始一年的促销活动。今年二月中旬会开启新春采购季的活动,我们已经看到腾讯云商家在春节期间已经有预告活动。当时已经看到有抢先优惠促销活动,目前我们企业和个人可以领取腾讯云代金券满减活动,以及企业用户可以领取域名优惠低至.COM域名1元。 直达链接 - 腾讯云新春采购活动抢先看活动时间:2022年1月20日至2022年2月15日我们可以在...
[六一云迎国庆]转盘活动实物礼品美国G口/香港CTG/美国T级超防云/物理机/CDN大促销六一云 成立于2018年,归属于西安六一网络科技有限公司,是一家国内正规持有IDC ISP CDN IRCS电信经营许可证书的老牌商家。大陆持证公司受大陆各部门监管不好用支持退款退现,再也不怕被割韭菜了!主要业务有:国内高防云,美国高防云,美国cera大带宽,香港CTG,香港沙田CN2,海外站群服务,物理机,...
atcloud怎么样?atcloud刚刚发布了最新的8折优惠码,该商家主要提供常规cloud(VPS)和storage(大硬盘存储)系列VPS,其数据中心分布在美国(俄勒冈、弗吉尼亚)、加拿大、英国、法国、德国、新加坡,所有VPS默认提供480Gbps的超高DDoS防御。Atcloud高防VPS。atcloud.net,2020年成立,主要提供基于KVM虚拟架构的VPS、只能DNS解析、域名、SS...