locked
C# XML to Class Object WinRT

    Question

  • Hello everybody,

    i have a server that has a database on it, when i send a request to it, it traverses through the database, gets the information (data) and sends a response of a constructed XML file.

    the XML file looks like this

    <ArrayOfVideo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <Video><ID>some id</ID>

    <VideoID>some other id</VideoID>

    <EnglishDescription>lorem ipsum :P</EnglishDescription>

    <EnglishTitle>same lorem ipsum</EnglishTitle>

    <ArabicDescription>again lorem ipusm</ArabicDescription>

    <ArabicTitle>more of lorem ipsum</ArabicTitle>

    </Video>

    <Video>.. etc

    basically its a list of videos with their attributes in tags like <EnglishDescription> or <ID> or other

    i was able to read the XML file this way, but i don't know how to take each of these nodes ex: <EnglishDescription> take the data inside it and put it in an object of class type countries.

    private async void itemGridView_Loaded(object sender, RoutedEventArgs e)
            {
                //string XMLPath = Path.Combine(Package.Current.InstalledLocation.Path, " http://xxxxxxxxxxxxxxxxx");
                //XDocument loadedData = XDocument.Load("http:xxxxxxxxxxxxxxxxxxxxxxx");
                //retriving data from xml using LINQ
                Uri uri = new Uri("http:xxxxxxxxxxxxxxxxxxxxx");
                HttpClient client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(uri);
                response.EnsureSuccessStatusCode();
    
                Stream stream = await response.Content.ReadAsStreamAsync();
                using (StreamReader sr = new StreamReader(stream, true))
                {
                    XDocument loadedData = XDocument.Load(sr);
    
                    // XDocument loadedData = XDocument.Load("xxxxxxxxxxxxxxxxxxxx");
    
                    var data = from query in loadedData.Descendants("Video")
                               select new Countries
                               {
                                   Title = (string)query.Attribute("EnglishTitle")
                               };
                    var gridview = (GridView)sender;
                    gridview.ItemsSource = data;
                }
            }

    basically i want to fill the attributes, can you please point me out :)

    i have objects of type country what i need to do is

    for example country.description = the content of the XML <EnglishDescription> tag

    thank you in advance




    Thursday, March 20, 2014 3:14 PM

Answers

  • Hi Anton,

    "EnglishDescription" and "EnglishTitle" are not attributes in your Xml document, they are child-elements. Thus, you cannot access them with query.Attribute("EnglishTitle"). Instead, you can use query.Element("EnglishTitle").Value to access the content of the xml element:

                var data = from query in loadedData.Descendants("Video")
                           select new Countries
                           {
                               Title = (string)query.Element("EnglishTitle").Value,
                               Description = query.Element("EnglishDescription").Value
    			   //... other properties
                           };


    Kind regards,
    Henning Dieterichs
    App-Entwickler-Hotline für MSDN Online Deutschland

    Disclaimer:
    Bitte haben Sie Verständnis dafür, dass wir hier auf Rückfragen gar nicht oder nur sehr zeitverzögert antworten können.
    Bitte nutzen Sie für Rückfragen oder neue Fragen den telefonischen Weg über die MSDN Hotline: http://www.msdn-online.de/Hotline
    MSDN Hotline: Schnelle & kompetente Hilfe für Entwickler: kostenfrei!

    Es gelten für die MSDN Hotline und dieses Posting diese Nutzungsbedingungen, Hinweise zu MarkenzeichenInformationen zur Datensicherheit sowie die gesonderten Nutzungsbedingungen für die MSDN Hotline.

    • Marked as answer by Anton Thabain Thursday, March 20, 2014 4:26 PM
    Thursday, March 20, 2014 4:17 PM

All replies

  • Hi Anton,

    "EnglishDescription" and "EnglishTitle" are not attributes in your Xml document, they are child-elements. Thus, you cannot access them with query.Attribute("EnglishTitle"). Instead, you can use query.Element("EnglishTitle").Value to access the content of the xml element:

                var data = from query in loadedData.Descendants("Video")
                           select new Countries
                           {
                               Title = (string)query.Element("EnglishTitle").Value,
                               Description = query.Element("EnglishDescription").Value
    			   //... other properties
                           };


    Kind regards,
    Henning Dieterichs
    App-Entwickler-Hotline für MSDN Online Deutschland

    Disclaimer:
    Bitte haben Sie Verständnis dafür, dass wir hier auf Rückfragen gar nicht oder nur sehr zeitverzögert antworten können.
    Bitte nutzen Sie für Rückfragen oder neue Fragen den telefonischen Weg über die MSDN Hotline: http://www.msdn-online.de/Hotline
    MSDN Hotline: Schnelle & kompetente Hilfe für Entwickler: kostenfrei!

    Es gelten für die MSDN Hotline und dieses Posting diese Nutzungsbedingungen, Hinweise zu MarkenzeichenInformationen zur Datensicherheit sowie die gesonderten Nutzungsbedingungen für die MSDN Hotline.

    • Marked as answer by Anton Thabain Thursday, March 20, 2014 4:26 PM
    Thursday, March 20, 2014 4:17 PM
  • thank you for your reply, i figured it out a minute before you posted and with your post i got 100% sure that i have done the right thing :D

    thank you again and i will mark it as an answer so that it might benefit others :D

    sincere regards,

    Anton

    Thursday, March 20, 2014 4:25 PM