Introduction to LINQ - Simple XML Parsing
2012-03-17 23:17
399 查看
Every programmer has had to parse an XML file. If you're trying to tell me you haven't, then you're lying and you just don't remember it. You probably shouldn't even call yourself a programmer.
If you don't remember the history of reading an XML document, here's the Cliffs Notes version. In the beginning there was line-by-line parsing. It was hard, error prone, and usually sucked. After a little while of this, light started shining onto our
dark corner of the world. .NET introduced the
XmlTextReader class, and things were good - for a while. And then, as if Microsoft heard the prayers of a million programmers,
LINQ was integrated into .NET 3.5 and again, things are good.
It's the anonymous types and methods introduced into .NET 3.5 that makes LINQ possible. If you haven't messed with anonymous types yet, I would highly recommend it. MSDN has some
good documentation.
Let's start out this tutorial by looking at some XML code. I've created a small document that outlines some of the tutorials we've created.
<?xml
version="1.0"
encoding="utf-8"
?>
<Tutorials>
<Tutorial>
<Author>The Reddest</Author>
<Title>
Creating an XP Style WPF Button with Silverlight
</Title>
<Date>2/20/2008</Date>
</Tutorial>
<Tutorial>
<Author>The Fattest</Author>
<Title>
Flex And Yahoo Maps
</Title>
<Date>2/12/2007</Date>
</Tutorial>
<Tutorial>
<Author>The Tallest</Author>
<Title>
WPF Tutorial - Creating A Custom Panel Control
</Title>
<Date>2/18/2008</Date>
</Tutorial>
</Tutorials>
First, we're going to use some simple LINQ syntax to create some anonymous objects with the properties, Author, Title, and Date.
XDocument xmlDoc
= XDocument.Load("TestFile.xml");
var tutorials =
from tutorial in xmlDoc.Descendants("Tutorial")
select
new
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = tutorial.Element("Date").Value,
};
The result of this code will be an IEnumerable collection filled with anonymous objects. What's nice about Visual Studio is that you'll even get dot completion for anonymous types.
Anonymous objects are well and good, but usually I want something a little better defined to pass around my application. Let's look at how to create a
public
class Tutorial
{
public string Author
{ get;
set;
}
public string Title
{ get;
set;
}
public DateTime Date
{ get;
set;
}
}
...
List<Tutorial> tutorials
=
(from tutorial
in xmlDoc.Descendants("Tutorial")
select new Tutorial
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = DateTime.Parse(tutorial.Element("Date").Value),
}).ToList<Tutorial>();
At this point, you're probably floored at how quickly I was just able to parse that XML document into my collection - I know I am. Let's look at one simple piece I haven't mentioned yet, the
List<Tutorial> tutorials
=
(from tutorial
in xmlDoc.Descendants("Tutorial")
where tutorial.Element("Author").Value
== "The Tallest"
select new Tutorial
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = DateTime.Parse(tutorial.Element("Date").Value),
}).ToList<Tutorial>();
Now my list of tutorials will only contain those written by The Tallest.
If you don't remember the history of reading an XML document, here's the Cliffs Notes version. In the beginning there was line-by-line parsing. It was hard, error prone, and usually sucked. After a little while of this, light started shining onto our
dark corner of the world. .NET introduced the
XmlTextReader class, and things were good - for a while. And then, as if Microsoft heard the prayers of a million programmers,
LINQ was integrated into .NET 3.5 and again, things are good.
It's the anonymous types and methods introduced into .NET 3.5 that makes LINQ possible. If you haven't messed with anonymous types yet, I would highly recommend it. MSDN has some
good documentation.
Let's start out this tutorial by looking at some XML code. I've created a small document that outlines some of the tutorials we've created.
<?xml
version="1.0"
encoding="utf-8"
?>
<Tutorials>
<Tutorial>
<Author>The Reddest</Author>
<Title>
Creating an XP Style WPF Button with Silverlight
</Title>
<Date>2/20/2008</Date>
</Tutorial>
<Tutorial>
<Author>The Fattest</Author>
<Title>
Flex And Yahoo Maps
</Title>
<Date>2/12/2007</Date>
</Tutorial>
<Tutorial>
<Author>The Tallest</Author>
<Title>
WPF Tutorial - Creating A Custom Panel Control
</Title>
<Date>2/18/2008</Date>
</Tutorial>
</Tutorials>
First, we're going to use some simple LINQ syntax to create some anonymous objects with the properties, Author, Title, and Date.
XDocument xmlDoc
= XDocument.Load("TestFile.xml");
var tutorials =
from tutorial in xmlDoc.Descendants("Tutorial")
select
new
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = tutorial.Element("Date").Value,
};
The result of this code will be an IEnumerable collection filled with anonymous objects. What's nice about Visual Studio is that you'll even get dot completion for anonymous types.
Anonymous objects are well and good, but usually I want something a little better defined to pass around my application. Let's look at how to create a
Listof
Tutorialobjects.
public
class Tutorial
{
public string Author
{ get;
set;
}
public string Title
{ get;
set;
}
public DateTime Date
{ get;
set;
}
}
...
List<Tutorial> tutorials
=
(from tutorial
in xmlDoc.Descendants("Tutorial")
select new Tutorial
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = DateTime.Parse(tutorial.Element("Date").Value),
}).ToList<Tutorial>();
At this point, you're probably floored at how quickly I was just able to parse that XML document into my collection - I know I am. Let's look at one simple piece I haven't mentioned yet, the
whereclause. Just like SQL, LINQ has the ability to filter results using the
wherekeyword. Let's filter down my collection to only tutorials written by The Tallest (his are always the best anyway).
List<Tutorial> tutorials
=
(from tutorial
in xmlDoc.Descendants("Tutorial")
where tutorial.Element("Author").Value
== "The Tallest"
select new Tutorial
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = DateTime.Parse(tutorial.Element("Date").Value),
}).ToList<Tutorial>();
Now my list of tutorials will only contain those written by The Tallest.
相关文章推荐
- Simple example for Linq To XML
- Linq to XML 之XElement的Descendants方法的新发现
- [导入]使用linq to xml 快速创建自己的Rss 之二 Syndication篇
- linq to xml(MOSS GetCurrentUser)
- [代码]如何从文件加载XML(LINQ to XML)
- [代码]捕获分析错误(LINQ to XML)
- LINQ to XML
- 步步为营VS 2008 + .NET 3.5(14) - XLINQ(LINQ to XML)之针对XML文件的添加、查询、更新和删除
- LINQ to XML操作Xml文档
- Linq to Xml,从其他xml文件读取节点(Element和Text混合)插入当前xml文件, 尖括号不转义
- LINQ to XML 编程基础
- linq to xml(添加节点3)
- LinQ to XML
- C# linq to xml
- 处理XML的新方式——LINQ to XML
- 使用Linq to XML 修改app.config
- simple introduction to FUSER
- LINQ To XML 入门(2)
- LINQ to XML 编程基础
- LINQ to XML 编程基础