0

Hi I am trying to parse some xml from a weird xml document developed by icalander. I have been having a lot of trouble just parsing the data, but thanks to the help of people from stackoverflow I have been able to parse the data. Now I need some help parsing between the nodes. Here is a link to the xml file I am parsing from (http://datastore.unm.edu/events/events.xml)

I am using the pivotapp model from Visual Studio 2010 to create this app. In the MainViewModel.cs section I am modifying the following code in hopes that the tag will print out in place of "LineOne" (code listed below). For example, from the xml file linked above, I would like LineOne = Lobo's Got Talent.

I need help figuring out the best method to achieve this, I will need LineTwo to contain the date and time, and LineThree to contain the description.

Thank you for your time and help, it has been greatly appreciated!

    public void LoadData()
    {
        var webClient = new WebClient();
        webClient.OpenReadAsync(new Uri("http://datastore.unm.edu/events/events.xml"));
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
    }

    public void webClient_OpenReadCompleted(object sender, 
                                            OpenReadCompletedEventArgs e)
    {
        XDocument unmXdoc = XDocument.Load(e.Result, LoadOptions.None);
        this.Items.Add(new ItemViewModel() { LineOne = unmXdoc.ToString(), 
                                             LineTwo = "", LineThree = "" });
    }

Thank you for looking and helping!

vector
  • 359
  • 1
  • 6
  • 12

1 Answers1

0

The xml is fine, I think you are running into a namespace issue here, you have two options, strip the namespace of the xml file if you are sure you do not need it. The preferred option is to work with the namespace and specify it for the fully qualified element names. see Here

 private readonly XNamespace dataNamspace = "urn:ietf:params:xml:ns:icalendar-2.0";

    public void webClient_OpenReadCompleted(object sender,
                                            OpenReadCompletedEventArgs e)
    {
        XDocument unmXdoc = XDocument.Load(e.Result, LoadOptions.None);

        this.Items = from p in unmXdoc.Descendants(dataNamspace + "vevent").Elements(dataNamspace + "properties")
                     select new ItemViewModel
            {
                LineOne = this.GetElementValue(p, "summary"),
                LineTwo = this.GetElementValue(p, "description"),
                LineThree = this.GetElementValue(p, "categories"),
            };

        lstData.ItemsSource = this.Items;
    }

    private string GetElementValue(XElement element, string fieldName)
    {
        var childElement = element.Element(dataNamspace + fieldName);

        return childElement != null ? childElement.Value : String.Empty;
    }
Community
  • 1
  • 1
FunksMaName
  • 2,101
  • 1
  • 15
  • 17
  • Hi, thank you for the response! I am having trouble with the "lstData.ItemsSource = this.Items;" What is lstdata supposed to be? – vector Nov 04 '13 at 23:19
  • I was assuming that you were binding "this.Items" to a ListBox? It looks like you are using an MVVM pattern, in which case, take the line off. Your new entries should now be reflected in the new UI. if this is not clear then post your XAML as well so that I can modify the answer accordingly. – FunksMaName Nov 05 '13 at 09:30
  • So I combined the code you wrote plus the code from the link you sent me along with a few more global constructors and everything is working perfectly! Thank you for the help! I would still be stuck without it! And yeah, I thought you were talking about a listbox, but for some reason when I tried to link to my FirstListBox I had an issue, so I was checking to make sure that's what you meant. – vector Nov 05 '13 at 17:17