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
之前几个月由于CHIA挖矿导致全球固态硬盘的价格疯涨,如今硬盘挖矿基本上已死,硬盘的价格基本上恢复到常规价位,所以,pacificrack决定对全系Cloud server进行价格调整,降幅较大,“如果您是老用户,请通过续费管理或升级套餐,获取同步到最新的定价”。官方网站:https://pacificrack.com支持PayPal、支付宝等方式付款VPS特征:基于KVM虚拟,纯SSD raid...
天上云怎么样?天上云隶属于成都天上云网络科技有限公司,是一家提供云服务器及物理服务器的国人商家,目前商家针对香港物理机在做优惠促销,香港沙田机房采用三网直连,其中电信走CN2,带宽为50Mbps,不限制流量,商家提供IPMI,可以自行管理,随意安装系统,目前E3-1225/16G的套餐低至572元每月,有做大规模业务的朋友可以看看。点击进入:天上云官方网站天上云香港物理机服务器套餐:香港沙田数据中...
青云互联怎么样?青云互联是一家成立于2020年6月的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,目前提供有美国免费主机、香港主机、香港服务器、美国云服务器,让您的网站高速、稳定运行。美国cn2弹性云主机限时8折起,可选1-20个IP,仅15元/月起,附8折优惠码使用!点击进入:青云互联官方网站地址青云互联优惠码:八折优惠码:ltY8sHMh (续费同价)青云互联活动方案:美国洛杉矶...