摘要:

正文:

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. python flask 接口

    例子1 from flask import Flask, jsonify app = Flask(__name__) tasks = [ { , 'title': u'Buy groceries', ...

  2. 通过 sqldf 包使用 SQL 查询数据框

    在前面的章节中,我们学习了如何编写 SQL 语句,在关系型数据库(如 SQLite 和MySQL )中查询数据.我们可能会想,有没有一种方法,能够直接使用 SQL 进行数据框查询,就像数据框是关系型数 ...

  3. logback的使用和logback.xml详解[转]

    一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它两 ...

  4. PHP求并集,交集,差集

    PHP求并集,交集,差集 一.总结 一句话总结:在php中如果我想要对两个数组进行如并集.交集和差集操作,我们可直接使用php自带的函数来操作如array_merge(),array_intersec ...

  5. 20170801xlVBA含有公式出现弹窗合并

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public Sub GatherD ...

  6. How to create own operator with python in mxnet?

    继承CustomOp 定义操作符,重写前向后向方法,此时可以通过_init__ 方法传递需要用到的参数 class LossLayer(mxnet.operator.CustomOp): def __ ...

  7. Xshell如何设置,当连接断开时保留Session,保留原文字

    Xshell [1]  是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议.Xshell 通过互联网到远程主机的安全连接以及它 ...

  8. python运维之使用python进行批量管理主机

    1. python运维之paramiko 2. FABRIC 一个与多台服务器远程交互的PYTHON库和工具 3. SSH连接与自动化部署工具paramiko与Fabric 4. Python批量管理 ...

  9. 原生JS和jQuery版实现文件上传功能

    <!doctype html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  10. OC ARC之循环引用问题(代码分析)

    // // main.m // 03-arc-循环引用 // // Created by apple on 13-8-11. // Copyright (c) 2013年 itcast. All ri ...