C#中XML文件数据如何保存在泛型集合中
比如有一XML文件:<?xmlversion="1.0"encoding="utf-8"?>-<ShowList>-<Movie><Name>功夫</Name><Post...
比如有一XML文件:
<?xml version="1.0" encoding="utf-8" ?>
- <ShowList>
- <Movie>
<Name>功夫</Name>
<Poster>gongfu.jpg</Poster>
<Director>周星驰</Director>
<Actor>周星驰</Actor>
<Type>Action</Type>
<Price>60</Price>
- <Schedule>
<Item>8-00 9-45</Item>
<Item>10-00 11-45</Item>
</Schedule>
</Movie>
- <Movie>
<Name>疯狂的石头</Name>
<Poster>bone.jpg</Poster>
<Director>宁浩</Director>
<Actor>郭涛</Actor>
<Type>Comedy</Type>
<Price>60</Price>
- <Schedule>
<Item>12-00 13-45</Item>
<Item>14-00 15-45</Item>
</Schedule>
</Movie>
- <Movie>
<Name>蜘蛛人3</Name>
<Poster>spiderman3.jpg</Poster>
<Director>萨姆·莱米</Director>
<Actor>托贝·马奎尔</Actor>
<Type>Action</Type>
<Price>60</Price>
- <Schedule>
<Item>16-00 17-45</Item>
<Item>18-00 19-45</Item>
</Schedule>
</Movie>
</ShowList>
有一泛型集合对象Dictionary<string, Movie> item,已定义好字段(例如:string name,string poster,int price等)如何保存到item中? 展开
<?xml version="1.0" encoding="utf-8" ?>
- <ShowList>
- <Movie>
<Name>功夫</Name>
<Poster>gongfu.jpg</Poster>
<Director>周星驰</Director>
<Actor>周星驰</Actor>
<Type>Action</Type>
<Price>60</Price>
- <Schedule>
<Item>8-00 9-45</Item>
<Item>10-00 11-45</Item>
</Schedule>
</Movie>
- <Movie>
<Name>疯狂的石头</Name>
<Poster>bone.jpg</Poster>
<Director>宁浩</Director>
<Actor>郭涛</Actor>
<Type>Comedy</Type>
<Price>60</Price>
- <Schedule>
<Item>12-00 13-45</Item>
<Item>14-00 15-45</Item>
</Schedule>
</Movie>
- <Movie>
<Name>蜘蛛人3</Name>
<Poster>spiderman3.jpg</Poster>
<Director>萨姆·莱米</Director>
<Actor>托贝·马奎尔</Actor>
<Type>Action</Type>
<Price>60</Price>
- <Schedule>
<Item>16-00 17-45</Item>
<Item>18-00 19-45</Item>
</Schedule>
</Movie>
</ShowList>
有一泛型集合对象Dictionary<string, Movie> item,已定义好字段(例如:string name,string poster,int price等)如何保存到item中? 展开
2个回答
展开全部
class Program
{
static void Main(string[] args)
{
Dictionary<string, Movie> ms = new Dictionary<string, Movie>();
XDocument doc = XDocument.Load("movies.xml");
var es = doc.Element("ShowList").Elements("Movie");
foreach (var e in es)
{
Movie movie = e.ToObject<Movie>();
if (movie.Name == null) continue;
ms.Add(movie.Name, movie);
}
Console.WriteLine(ms.Count);
Console.ReadLine();
}
}
public static class ExParse
{
public static T ToObject<T>(this XElement xe)
{
T local = Activator.CreateInstance<T>();
PropertyInfo[] propertys = typeof(T).GetProperties();
foreach (var p in propertys)
{
if (!p.CanWrite) continue;
XElement e = xe.Element(p.Name);
if (e == null) continue;
p.SetValue(local, Convert.ChangeType(e.Value, p.PropertyType), null);
}
return local;
}
}
{
static void Main(string[] args)
{
Dictionary<string, Movie> ms = new Dictionary<string, Movie>();
XDocument doc = XDocument.Load("movies.xml");
var es = doc.Element("ShowList").Elements("Movie");
foreach (var e in es)
{
Movie movie = e.ToObject<Movie>();
if (movie.Name == null) continue;
ms.Add(movie.Name, movie);
}
Console.WriteLine(ms.Count);
Console.ReadLine();
}
}
public static class ExParse
{
public static T ToObject<T>(this XElement xe)
{
T local = Activator.CreateInstance<T>();
PropertyInfo[] propertys = typeof(T).GetProperties();
foreach (var p in propertys)
{
if (!p.CanWrite) continue;
XElement e = xe.Element(p.Name);
if (e == null) continue;
p.SetValue(local, Convert.ChangeType(e.Value, p.PropertyType), null);
}
return local;
}
}
追问
var es .....什么意思?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-04-02
展开全部
在百度里搜“冰河软件开发视频”打开第一个链接,里面有一个视频专们讲C#如何读取XML文件到对象
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询