简体   繁体   中英

Delving into the world of XML (Windows Phone) Error I dont understand (The ' ' character, hexadecimal value 0x20, cannot be included in a name.)

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 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. (The bit in * is where it says the error is)

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

Edit: Answer below fixed the error :D However still nothing is coming up. I "think" it's because of the XML layout and the amount of descendants it has (Cant work out what I need to do being a noob at XML and pulling it from the web as a data source)

Maybe I am starting too complicated :/

Still any help/tips on how to pull some elements from the feed (As there all in Descendants) correctly and store them would be great :D

Edit2: I have it working (In a crude way) but still :D

Thanks Adam Maras!

The last issue was the double listing. (Adding it to a list, to then add it to another list was causing a null exception) Just using the 1 list within the method solved this issue, (Probably not the best way of doing it but it works for now) and allowed for me to add the results to a listbox until I spend some time working out how to use ListBox.ItemTemplate & DataTemplate to make it look more appealing. (Seems easy enough I say now...)

Thanks Again!!!

from item in xdoc.Descendants("situation id")
//                                      ^

XML tag names can't contain spaces. Looking at the XML, you probably just want "situation" to match the <situation> elements.


After looking at your edit and further reviewing the XML, I figured out what the problem is. If you look at the root element of the document:

<d2LogicalModel xmlns="http://datex2.eu/schema/1_0/1_0" modelBaseVersion="1.0">

You'll see that it has a default namespace applied. The easiest solution to your problem will be to first get the namespsace from the root element:

var ns = xdoc.Root.Name.Namespace;

And then apply it wherever you're using a string to identify an element or attribute name:

from item in xdoc.Descendants(ns + "situation")
// ...
item.Element(ns + "impactOnTraffic").Value
item.Element(ns + "trafficRestrictionType").Value

One more thing: <impactOnTraffic> and <trafficRestrictionType> aren't direct children of the <situation> element, so you'll need to change that code as well:

Title = items.Descendants(ns + "impactOnTraffic").Single().Value,
Description = item.Descendants(ns + "trafficRestrictionType").Single().Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM