摘要:

正文:

1.引入XDocument的命名空间

using System.Xml.Linq;

2. List<CourseItem> to XML doc

        //List<CourseItem> to XML
public XDocument InitDownloadData(List<CourseItem> item)
{
XElement courseItemElement = new XElement("Courses",
item.Select(c => new XElement("Course",
new XAttribute("Title", c.Title),
new XElement("Description", c.Description),
new XElement("Owner", c.Owner),
new XElement("PublishDate", c.PublishDate),
new XElement("Rating", c.Rating),
new XElement("TotalPoints", c.TotalPoints),
new XElement("Modules",
c.Modules.Select(m => new XElement("Module",
new XAttribute("Title", m.Title),
new XElement("Description", m.Description),
new XElement("Points", m.Points),
new XElement("Materials",
m.Materials.Select(ma => new XElement("Material",
new XAttribute("Title", ma.Title),
new XElement("Id", ma.Id),
new XElement("MType", ma.MType))))))),
new XElement("ID", c.ID))));
XDocument doc = new XDocument(courseItemElement); if (doc != null)
return doc;
else return null; }

3. XML to List<CourseItem>

        //XML to List<CourseItem> Note: doc 是由List<CourseItem> 转化而来
public List<CourseItem> DeserializeToClass(XDocument doc)
{
if (doc != null)
{
var courses =
(from c in doc.Descendants("Course")
select new CourseItem
{
Title=c.Attribute("Title").Value ,
ID = c.Element("ID").Value,
Description =c.Element ("Description").Value ,
Owner = c.Element("Owner").Value,
TotalPoints =c.Element ("TotalPoints").Value ,
PublishDate =DateTime.Parse(c.Element ("PublishDate").Value),
Rating=c.Element ("Rating").Value ,
Modules = (from m in c.Descendants("Module")
select new Module
{
Title = m.Attribute("Title").Value,
Description=m.Element ("Description").Value,
Points =m.Element ("Points").Value ,
Materials = (from ma in m.Descendants("Material")
select new Material {
Title = ma.Attribute("Title").Value,
Id = ma.Element("Id").Value, //string to enum conversion in C#
MType=(MaterialType)Enum.Parse (typeof(MaterialType),ma.Element ("MType").Value )
}).ToList()
}).ToList()
}).ToList(); if (courses != null)
return courses;
}
return null;
}

4. CourseItem, Module, Material类型定义

    class CourseItem
{
private string _title;
public string Title { get { return _title; } set { _title = value; } } private string _id;
public string ID { get { return _id; } set { _id = value; } } private string _description;
public string Description { get { return _description; } set { _description = value; } } private string _totalPoints;
public string TotalPoints { get { return _totalPoints; } set { _totalPoints = value; } } private string _level;
public string Level { get { return _level; } set { _level = value; } } private string _owner;
public string Owner { get { return _owner; } set { _owner = value; } } private string _rating;
public string Rating { get { return _rating; } set { _rating = value; } } private Category _category;
public Category Category { get { return _category; } set { _category = value; } } private DateTime _pubDate;
public DateTime PublishDate { get { return _pubDate; } set { _pubDate = value; } } public List<Module> Modules { get; set; }
} public class Module
{
public string Title { get; set; }
public string Description { get; set; }
public string Points { get; set; }
public List<Material> Materials { get; set; }
} public class Material
{
public string Title { get; set; }
public string Id { get; set; }
public MaterialType MType { get; set; }
}

参考:

http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument?rq=1

http://stackoverflow.com/questions/1187085/string-to-enum-conversion-in-c-sharp

XDocument 使用的更多相关文章

  1. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  2. XDocument获取指定节点

    string xmlFile = @"D:\Documents\Visual Studio 2013\Projects\Jesee.Web.Test\ConsoleApplication1\ ...

  3. C# XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  4. XDocument 获取包括第一行的声明(版本、编码)的所有节点

    XDocument保存为xml文件的方法如下: XDocument doc = new XDocument( new XDeclaration("1.0","UTF-8& ...

  5. 将XmlDocument转换成XDocument

    XmlDocument xml=new XmlDocument(); xml.LoadXml(strXmlText); XmlReader xr=new XmlNodeReader(xml); XDo ...

  6. WPF 关于XDocument(xml) 的部分操作记录

    (1)删除xml文件中的一个结点的方法,有如下两种方式(只有存在数据绑定的情况下才会有第二种情况,否则一般是第一种情况): private void DeletePacsNode() { //从xml ...

  7. .Net 4.0 Convert Object to XDocument

    将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...

  8. XDocument和XmlDocument的区别

    刚开始使用Xml的时候,没有注意到XDocument和XmlDocument的区别,后来发现两者还是有一些不同的. XDocument和XmlDocument都可以用来操作XML文档,XDocumen ...

  9. 05-XML遍历递归显示到TreeView上(XDocument类)

    1.XML文件(x1.xml): <?xml version="1.0" encoding="utf-8" ?> <itcast> &l ...

  10. XmlDocument,XDocument相互转换

    XmlDocument,XDocument相互转换 using System; using System.Xml; using System.Xml.Linq; namespace MyTest { ...

随机推荐

  1. hihocoder 九十八周 搜索一 24点

    题目1 : 搜索一·24点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 周末,小Hi和小Ho都在家待着. 在收拾完房间时,小Ho偶然发现了一副扑克,于是两人考虑用这副 ...

  2. 《Robot Framework自动化测试修炼宝典》道长

    1. Python下载https://www.python.org/downloads 2. Setuptools下载https://pypi.python.org/pypi/setuptools用原 ...

  3. STL__容器的分类

    1.序列式 vector.deque.list 2.关联式 set.multiset.map.multimap 3. ZC: queue.stack 属于什么类型?序列式? 4. 5.

  4. Android蓝牙通信功能开发

    1. 概述 Bluetooth 是几乎现在每部手机标准配备的功能,多用于耳机 mic 等设备与手机的连接,除此之外,还可以多部手机之间建立 bluetooth 通信,本文就通过 SDK 中带的一个聊天 ...

  5. Codeforces 294D - Shaass and Painter Robot

    294D - Shaass and Painter Robot 思路: 可以用数学归纳法证明一个结论:整个棋盘黑白相间当且仅当边缘黑白相间. 分奇偶讨论又可得出边缘黑色格个数为n+m-2 这样就可以暴 ...

  6. Python -- Scrapy 架构概览

    架构概览 本文档介绍了Scrapy架构及其组件之间的交互. 概述 接下来的图表展现了Scrapy的架构,包括组件及在系统中发生的数据流的概览(绿色箭头所示). 下面对每个组件都做了简单介绍,并给出了详 ...

  7. ubuntu10.04 交叉编译 aria2 总结

    1) google之后,找到 这个 https://github.com/z24/pitv/tree/master/cross 的脚本, 觉得非常好. 于是准备用来进行编译 2) 安装交叉编译器 su ...

  8. 读CSV转换datatable

    using System.Data; using System.IO;   /// <summary> /// Stream读取.csv文件 /// </summary> // ...

  9. 使用Jenkins实现maven项目一键部署

    下面的博客请详细的,值得一看:jenkins+maven+svn实现简单的一键发布 http://blog.csdn.net/pein_zero/article/details/52597615#co ...

  10. 广播 (Broadcast)

    广播 :在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内 ...