摘要:

正文:

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. hdu 5144 NPY and shot 物理+三分

    NPY and shot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pro ...

  2. hdu 2824 The Euler function 欧拉函数打表

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. Python 获取文件的创建时间,修改时间和访问时间

    # 用到的知识# os.path.getatime(file) 输出文件访问时间# os.path.getctime(file) 输出文件的创建时间# os.path.getmtime(file) 输 ...

  4. Qt5.3.2_CentOS6.4_基本编程环境__20160306【勿删,繁琐】

    20160306 全程没有f/q ZC:使用的虚拟机环境是:博客园VMwareSkill 的 “CentOS6.4_x86_120g__20160306.rar” 1. 执行命令“gcc -v”,显示 ...

  5. VcCallC#_01

    1.C# 代码: using System; using System.Collections.Generic; //using System.Linq; using System.Text; //u ...

  6. ehcache入门基础示例

    一:目录 EhCache 简介 Hello World 示例 Spring 整合 二: 简介 1. 基本介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernat ...

  7. Android 7.0真实上手体验

    Android 7.0真实上手体验 Android 7.0的首个开发者预览版发布了,支持的设备只有Nexus6.Nexus 5X.Nexus 6P.Nexus 9.Nexus Player.Pixel ...

  8. C# / .NET 在类中使用Server.MapPath

    直接在类中使用 Server.MapPath 会出现错误,这是由于类中不能直接使用 System.Web.UI.Page 的非静态函数造成的.解决方法有两种: 方法一.使类继承System.Web.U ...

  9. MySQL解析过程、执行过程

    转载:https://student-lp.iteye.com/blog/2152601 https://www.cnblogs.com/cdf-opensource-007/p/6502556.ht ...

  10. consumer的DubboClientHandler线程池

    1. 创建线程池 创建线程池的调用栈如下: SimpleDataStore把线程池存放在map中. public class NettyClient extends AbstractClient { ...