XDocument 使用

时间:2024-09-05 08:37:08

摘要:

正文:

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://*.com/questions/1542073/xdocument-or-xmldocument?rq=1

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