Delving into the world of XML (Windows Phone) Error I dont understand (The
' ' character, hexadecimal value 0x20, cannot be included in a...
So I am starting to learn how to use XML data within a app and decided to
use some free data to do this however I cannot for the life of me get it
working this is my code so far. (I have done a few apps with static data
before but hey apps are designed to use the web right? :p)
public class XmlItem
{
public string Title { get; set; }
public string Description { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
List<XmlItem> xmlItems = new List<XmlItem>();
// Constructor
public MainPage()
{
InitializeComponent();
LoadXmlItems("http://hatrafficinfo.dft.gov.uk/feeds/datex/England/CurrentRoadworks/content.xml");
test();
}
public void test()
{
foreach (XmlItem item in xmlItems)
{
testing.Text = item.Title;
}
}
public void LoadXmlItems(string xmlUrl)
{
WebClient client = new WebClient();
client.OpenReadCompleted += (sender, e) =>
{
if (e.Error != null)
return;
Stream str = e.Result;
XDocument xdoc = XDocument.Load(str);
xmlItems = (from item in xdoc.Descendants("situation id")
select new XmlItem()
{
Title =
item.Element("impactOnTraffic").Value,
Description =
item.Element("trafficRestrictionType").Value
}).ToList();
// close
str.Close();
// add results to the list
xmlItems.Clear();
foreach (XmlItem item in xmlItems)
{
xmlItems.Add(item);
}
};
client.OpenReadAsync(new Uri(xmlUrl, UriKind.Absolute));
}
}
I am basically trying to learn how to do this at the moment as I am
intrigued how to actually do it (I know there are many ways but ATM this
way seems the easiest) I just don't get what the error is ATM.
I also know the display function ATM is not great (As it will only show
the last item) but for testing this will do for now.
To some this may seem easy, as a learner its not so easy for me just yet.
The error in picture form: (It seems I cant post images :/)
Thanks in advance for the help
No comments:
Post a Comment