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
昔日数据,国内商家,成立于2020年,主要销售湖北十堰和香港HKBN的云服务器,采用KVM虚拟化技术构架,不限制流量。当前夏季促销活动,全部首月5折促销,活动截止于8月11日。官方网站:https://www.xrapi.cn/5折优惠码:XR2021湖北十堰云服务器托管于湖北十堰市IDC数据中心,母鸡采用e5 2651v2,SSD MLC企业硬盘、 rdid5阵列为数据护航,100G高防,超出防...
我们在选择虚拟主机和云服务器的时候,是不是经常有看到有的线路是BGP线路,比如前几天有看到服务商有国际BGP线路和国内BGP线路。这个BGP线路和其他服务线路有什么不同呢?所谓的BGP线路机房,就是在不同的运营商之间通过技术手段时间各个网络的兼容速度最佳,但是IP地址还是一个。正常情况下,我们看到的某个服务商提供的IP地址,在电信和联通移动速度是不同的,有的电信速度不错,有的是移动速度好。但是如果...
快云科技: 11.11钜惠 美国云机2H5G年付148仅有40台,云服务器全场7折,香港云服务器年付388仅不到五折 公司介绍:快云科技是成立于2020年的新进主机商,持有IDC/ICP/ISP等证件资质齐全主营产品有:香港弹性云服务器,美国vps和日本vps,香港物理机,国内高防物理机以及美国日本高防物理机官网地址:www.345idc.com活动截止日期为2021年11月13日此次促销活动提供...