zoukankan      html  css  js  c++  java
  • XDocument 使用

    摘要:

    正文:

    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

  • 相关阅读:
    Web服务器安全设置
    java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
    Java IO和Java NIO在文件拷贝上的性能差异分析
    Java高效读取大文件
    NIO入门之轻松读取大文件
    我来说说java的NIO
    java读取大文件 超大文件的几种方法
    @RequestBody 的正确使用办法
    友鱼项目知识点
    怎样查看Tomcat动态控制台信息
  • 原文地址:https://www.cnblogs.com/qixue/p/3227045.html
Copyright © 2011-2022 走看看