我有这个xml:
<cats>
<cat>
<name>funycat_00</name>
<category>funy</category>
<link>/data/funy/funy_cat00</link>
</cat>
<cat>
<name>funycat_01</name>
<category>funy</category>
<link>/data/funy/funy_cat01</link>
</cat>
<cat>
<name>funycat_02</name>
<category>funy</category>
<link>/data/funy/funy_cat02</link>
</cat>
<cat>
<name>funycat_03</name>
<category>funy</category>
<link>/data/funy/funy_cat03</link>
</cat>
</cats>
我的课程:
private string _title;
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _picture;
public string Picture
{
get { return _picture; }
set { _picture = value; }
}
private string _category;
public string Category
{
get { return _category; }
set { _category = value; }
}
private List<Catpict> _listpict;
public List<Catpict> Listpict
{
get { return _listpict;}
set { _listpict = value; }
}
public List<Catpict> feed()
{
Catpict tempcat = new Catpict();
var reader = XDocument.Load("Data/DataFile.xml");
IEnumerable<XElement> cats = reader.Elements();
foreach (var cat in cats)
{
tempcat.Title = cat.Element("Name").Value;
Listpict.Add(tempcat);
}
return Listpict;
}
}
}
问题在于解析我的XML。 我想在我的xml中将所有cat发送到我的集合Listpict中。
问题可能很愚蠢,我的英语真的很糟糕。
如果有人对我有一些答案我完全开放。
Catpic是我的班级名字。
答案 0 :(得分:0)
我想你想要这样的东西......
public static List<CatPict> feed()
{
CatPict tempcat = new CatPict();
string xml = XDocument.Load("XMLFile1.xml").ToString();
using (XmlReader reader = new XmlTextReader(new StringReader(xml)))
{
while (reader.Read())
{
//put your logic here
}
}
return Listpict;
}