This example is an experiment that reads XML for my pilot project "Slshowcase". See the garden friend to do the Imitation 163 slides of the demo will be sent up in advance.
The function of the project is to do the case demonstration.
OK first, first define the XML.
projects.xml
<?xml version="1.0" encoding="utf-8" ? >
<projects>
<project id="1" title="nasa.wang @ cnblogs" category="web" hit="23" createdate="2008-4-9" converpic="null" iscommend="true" votenum="8" score="10">
</project>
</projects>
Define classes in SL.
public class project
{
public int id { get; set; }
public string title { get; set; }
public string category { get; set; }
public int hit { get; set; }
public DateTime createdate { get; set; }
public string converpic { get; set; }
public bool iscommend { get; set; }
public int votenum { get; set; }
public int score { get; set; }
//public IList<pitem> pitems { get; set; }
//public IList<pcomment> pcomments { get; set; }
}
Use WebClient to load data. LINQ to format the data.
public Page()
{
InitializeComponent();
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri (HtmlPage.Document.DocumentUri, "projects.xml"));
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler (client_DownloadStringCompleted);
}
void client_DownloadStringCompleted (object sender, DownloadStringCompletedEventArgs e)
{
XmlReader reader = xmlreader.create (new StringReader (E.result));
XDocument document = Xdocument.load (reader);
var projects = from F in document. Descendants ("project")
Select New Project
{
id = Int. Parse (F.attribute ("id"). Value),
title = F.attribute ("title"). Value,
Category = F.attribute ("category"). Value,
hit = Int. Parse (F.attribute ("hit"). Value),
CreateDate = DateTime.Parse (F.attribute ("CreateDate"). Value),
Converpic = F.attribute ("Converpic"). Value,
Iscommend = bool. Parse (F.attribute ("Iscommend"). Value),
votenum = Int. Parse (F.attribute ("Votenum"). Value),
score = Int. Parse (F.attribute ("Score"). Value),
};
list<project> _projects = new list<project> ();
_projects. AddRange (projects);
Txtstatus.text = "Read success for" + _projects[0].title;
}
Read finished. ;)
Following source: http://www.bianceng.net/dotnet/201212/685.htm