C#操作Xml:linq to xml操作XML
class Program{ static 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("color", "black")), new XElement("cat"), new XElement("pig", "pig is great"))); //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312 //默认是缩进格式化的xml,而无须格式化设置 xDoc.Save(Console.Out); Console.Read(); }} |
<?xml version="1.0" encoding="gb2312"?><root> <dog color="black">dog said black is a beautify color</dog> <cat /> <pig>pig is great</pig></root> |
class Program{ static 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("color", "black")), new XElement("cat"), new XElement("pig", "pig is great"))); //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312 //默认是缩进格式化的xml,而无须格式化设置 xDoc.Save(Console.Out); Console.WriteLine(); var query = from item in xDoc.Element( "root").Elements() select new { TypeName = item.Name, Saying = item.Value, Color = item.Attribute("color") == null?(string)null:item.Attribute("color").Value }; foreach (var item in query) { Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing"); } Console.Read(); }} |
3. Linq to xml简单的应用
应用需求: 读取博客园的rss,然后在页面上输出最新的10篇博客信息
实现要点: 通过XDocument的Load静态方法载入Xml,通过linq查询最新10条数据
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" %><script runat="server"> protected override void OnLoad(EventArgs e) { //实际应用,通过读取博客园的RSS生成Html代码显示最新的博客列表 //使用XDocument的Load静态方法载入Xml //玉开技术博客 http://www.cnblogs.com/yukaizhao var rssXDoc = XDocument.Load("http://www.cnblogs.com/rss"); //使用linq to xml查询前10条新博客 var queryBlogs = (from blog in rssXDoc.Descendants("item") select new { Title = blog.Element("title").Value, Url = blog.Element("link").Value, PostTime = DateTime.Parse(blog.Element("pubDate").Value) }).Take(20); repeaterBlogs.DataSource = queryBlogs; repeaterBlogs.DataBind(); base.OnLoad(e); }</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><head runat="server"> <title>Linq to Xml 实例</title></head><body> <ol> <asp:Repeater ID="repeaterBlogs" EnableViewState="false" runat="server"> <ItemTemplate> <li><span style="float: right"> <%#Eval("PostTime") %></span><a href="<%#Eval("Url") %>"><%#Eval("Title") %></a></li> </ItemTemplate> </asp:Repeater> </ol></body></html> |
C#处理Xml的相关随笔:
C#操作Xml:linq to xml操作XML的更多相关文章
- C# Xml Linq XDocument 基本操作 -- 重新学习
person.xml <?xml version="1.0" encoding="utf-8"?> <MyP> <P1> & ...
- XML操作之Linq to Xml
需要引用的命名空间: using System.Xml.Linq; 常用的类:XDocument.XElement.XAttribute 创建 XDocument对象. XDocument.Loa ...
- C#操作Xml:使用XmlReader读Xml
XmlDocument和XElement在读取Xml时要将整个Xml文档放到内存中去操作,这样做操作简单,但是很费内存和IO(可能是磁盘IO或者网络IO):而在有些场景下我们必须考虑尽可能节省内存和I ...
- 【转】C#对XML文件的各种操作实现方法
[转]C#对XML文件的各种操作实现方法 原文:http://www.jb51.net/article/35568.htm XML:Extensible Markup Language(可扩展标记语言 ...
- 配置文件操作(ini、cfg、xml、config等格式)
配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...
- php对xml文件进行CURD操作
XML是一种数据存储.交换.表达的标准: - 存储:优势在于半结构化,可以自定义schema,相比关系型二维表,不用遵循第一范式(可以有嵌套关系): - 交换:可以通过schema实现异构数据集成: ...
- 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】
一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...
- 【JAVA解析XML文件实现CRUD操作】
一.简介. 1.xml解析技术有两种:dom和sax 2.dom:Document Object Model,即文档对象模型,是W3C组织推荐的解析XML的一种方式. sax:Simple API f ...
- C# 使用XmlDocument类对XML文档进行操作
原创地址:http://www.cnblogs.com/jfzhu/archive/2012/11/19/2778098.html 转载请注明出处 W3C制定了XML DOM标准.很多编程语言中多提供 ...
- XML 文档解析操作
sing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security; ...
随机推荐
- C#访问Java的WebService添加SOAPHeader验证的问题
原文:C#访问Java的WebService添加SOAPHeader验证的问题 这两天做与公司OA的接口,发现C#访问Java的WebService需要提供一个SOAP的头验证信息,但是WebServ ...
- Hello ASP.NET5
2015年11月30日, ASP.NET 5 RC1 已经发布,本文尝试了一下ASP.NET5项目的创见一发布到IIS.开发环境,win10 64位,visual studio2015(已更新upda ...
- atitit.(设计模式1)--—职责链(chain of responsibility)最佳实践O7 转换日期
atitit.设计模式(1)---职责链模式(chain of responsibility)最佳实践O7 日期转换 1. 需求:::日期转换 1 2. 能够选择的模式: 表格模式,责任链模式 1 3 ...
- POJ 3009-Curling 2.0(DFS)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 5125 Desc ...
- MyEclipse—怎样在MyEclipse中创建servlet3.0
servlet3.0随着Java EE6规范一起公布,那么怎样在MyEclipse中创建3.0版的servlet呢? 1.启动MyEclipse.获得下图: 2.点击上图"File" ...
- Java常见Exception物种
Java常见Exception物种 1.ClassNotFoundException 2.IOException 3.NoSuchFieldException 4.NoSuchMethodExcept ...
- unity3d 学习笔记(两)
AudioClip 使用声音资源 unity3d资源可以被设置为声3d声音或2d声音.3d间的影响,越近声音越大 component: Audio source: 声音的发生物体 Audio list ...
- UVA10375 Choose and divide 质因数分解
质因数分解: Choose and divide Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- SDUT 1265-马停下过河卒(DFS)
马拦过河卒 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit3000ms Memory Limit 65536K ...
- [置顶] think in java interview-高级开发人员面试宝典(二)
从现在开始,以样题的方式一一列出各种面试题以及点评,考虑到我在前文中说的,对于一些大型的外资型公司,你将会面临全程英语面试,因此我在文章中也会出现许多全英语样题. 这些题目来自于各个真实的公司,公司名 ...