Silverlight Reads XML

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.