摘要:

正文:

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. linux——通信指令学习简单笔记

    一: 指令名称:write 指令所在路径:/usr/bin/write 执行权限:All User 语法:write <用户名> 功能描述:向另外一个用户发信息,以Ctrl+D作 为结束 ...

  2. python 同步与异步性能区别

    import gevent def task(pid): """ Some non-deterministic task """ geven ...

  3. Java实现邮箱激活验证

    最近从项目分离出来的注册邮箱激活功能,整理一下,方便下次使用 RegisterValidateService.java [java] view plaincopyprint?   package co ...

  4. SSD论文理解

    SSD论文贡献: 1. 引入了一种单阶段的检测器,比以前的算法YOLO更准更快,并没有使用RPN和Pooling操作: 2. 使用一个小的卷积滤波器应用在不同的feature map层从而预测BB的类 ...

  5. Codeforces 285C - Building Permutation

    285C - Building Permutation 思路:贪心.因为每个数都不同且不超过n,而且长度也为n,所有排列只能为1 2 3 ......n.所以排好序后与对应元素的差值的绝对值加起来就是 ...

  6. 雷林鹏分享:C# 预处理器指令

    C# 预处理器指令 预处理器指令指导编译器在实际编译开始之前对信息进行预处理. 所有的预处理器指令都是以 # 开始.且在一行上,只有空白字符可以出现在预处理器指令之前.预处理器指令不是语句,所以它们不 ...

  7. TCP三次握手(待细研究)

    xu言: 看到一张不错清晰的Tcp三次握手图,收藏 Initiator  发起人 Receiver  接收者 LISTENING 状态xx服务启动后首先处于侦听(LISTENING)状态. ESTAB ...

  8. C# 字符串 相关操作

    你或许知道你能使用String.Trim方法去除字符串的头和尾的空格,不幸运的是. 这个Trim方法不能去除字符串中间的C#空格. static void Main()         {       ...

  9. MVC ——设置启动 URL

    Visual Studio 会以一种有助的尝试,根据当前正在编辑的视图,让浏览器请求一个 URL.但这是一个不稳定的特性. 为了对浏览器的请求设置一个固定的 URL,可以从 Visual Studio ...

  10. redhat linux 6.2 安装配置GUI

    redhat6.2默认不安装GUI,启动时默认进入text模式,下面介绍下安装.配置GUI的步骤: 1.登录root 2.配置及测试yum   vi /etc/yum.repos.d/rhel-sou ...