locked
XML READING HAS ME PULLING MY HAIR, I HAVE NO HAIR LEFT ANYMORE..........

    Question

  • Before you think about sending me to some website, I have researched everywhere... Literally EVERYWHERE. First here is a sample of the xml file I want to read...

    <User_Library>
    <Music_File>
    <UniqueId>b7850389-6eed-4fef-a432-aa0204574103</UniqueId>
    <Album>Unknown</Album>
    <Artist/>
    <Genre>Unknown</Genre>
    <Title>(fdvm Remix)</Title>
    <Year>Unknown</Year>
    <File_Path>
    E:\Entertainment\Music\8 tracks playlist\Meltdown\(FDVM Remix).mp3
    </File_Path>
    <Is_Favorite>false</Is_Favorite>
    </Music_File>
    <Music_File>
    <UniqueId>009cd3ed-cf84-47ab-8df8-f9edc303ab8f</UniqueId>
    <Album>Chill Step Top 50</Album>
    <Artist>Capox</Artist>
    <Genre>Unknown</Genre>
    <Title>[old Track] River Flows In You</Title>
    <Year>2014</Year>
    <File_Path>
    E:\Entertainment\Music\8 tracks playlist\chill step top 50\[old track] river flows in you (capox remix).mp3
    </File_Path>
    <Is_Favorite>false</Is_Favorite>
    </Music_File>
    </User_Library>

    And here is the code I am using to read the xml file

     public async Task LoadUserLibrary()
            {
    
                var file = await ApplicationData.Current.LocalFolder.GetFileAsync("_userLibrary.xml");
                var doc = new XmlDocument();
                doc.LoadXml(await FileIO.ReadTextAsync(file));
                var nodeList = doc.SelectNodes("//Music_File");
               foreach (var node  in nodeList)
                {
                    Guid uniqueId;
                    Guid.TryParse(node.SelectSingleNode("//UniqueId").InnerText, out uniqueId);
                    var album = node.SelectSingleNode("//Album").InnerText;
                    var artist = node.SelectSingleNode("//Artist").InnerText;
                    var genre = node.SelectSingleNode("//Genre").InnerText;
                    var title = node.SelectSingleNode("//Title").InnerText;
                    uint year;
                    uint.TryParse(node.SelectSingleNode("//Year").InnerText, out year);
                    var filePath = node.SelectSingleNode("//File_Path").InnerText;
                    bool isFavorite;
                    bool.TryParse(node.SelectSingleNode("//Is_Favorite").InnerText, out isFavorite);
                    var musicItem = new MusicDataItem(uniqueId, album, artist, genre, title, year, filePath)
                    {
                        IsFavorite = isFavorite
                    };
                    MusicDataSource.Music.Add(musicItem);
                }
            }

    Now, my problem isn't that the code is not working or anything my problem is the it only returns the first nodes attributes times the number of nodes in the file. For example from the xml provided above it will only show say title i.e (fdvm Remix) two times instead of showing (fdvm Remix) and [old Track] River Flows In You....

    Help me stop pulling my hair.....


    M.K.N

    Wednesday, February 11, 2015 7:26 AM

Answers

  • Hi Mbithi Kioko,

    I used your code to read the xml and success in finding those data.

    You’ve not mentioned what’s the MusicDataSoruce object, I assume that’s the problem. Please check the Music collection after foreach statement to see if the xml data has been filled.

    If you still cannot make it work, please post more information or a mini repro project.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Mbithi Kioko Thursday, February 12, 2015 6:14 AM
    Thursday, February 12, 2015 5:31 AM
    Moderator

All replies

  • Hi Mbithi Kioko,

    I used your code to read the xml and success in finding those data.

    You’ve not mentioned what’s the MusicDataSoruce object, I assume that’s the problem. Please check the Music collection after foreach statement to see if the xml data has been filled.

    If you still cannot make it work, please post more information or a mini repro project.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by Mbithi Kioko Thursday, February 12, 2015 6:14 AM
    Thursday, February 12, 2015 5:31 AM
    Moderator
  • First off, thank you.... I was looking for the needle in the wrong hay stack....

    The problem was here

    Guid uniqueId;
                    Guid.TryParse(node.SelectSingleNode("//UniqueId").InnerText, out uniqueId);
                    var album = node.SelectSingleNode("//Album").InnerText;
                    var artist = node.SelectSingleNode("//Artist").InnerText;
                    var genre = node.SelectSingleNode("//Genre").InnerText;
                    var title = node.SelectSingleNode("//Title").InnerText;
                    uint year;
                    uint.TryParse(node.SelectSingleNode("//Year").InnerText, out year);
                    var filePath = node.SelectSingleNode("//File_Path").InnerText;
                    bool isFavorite;
                    bool.TryParse(node.SelectSingleNode("//Is_Favorite").InnerText, out isFavorite);
    those values weren't changing despite the fact that the inner text of the node was changing... I will let you know if I manage to fix it..


    M.K.N

    Thursday, February 12, 2015 6:13 AM
  • And this did the trick, Instead of relying on the node.SelectSingleNode("your node name") I have opted to use indexes (since the xml file will always have a constant number of childnodes in every Music_File node i.e 8) and it works like magic....

    Guid.TryParse(node.FirstChild.InnerText, out uniqueId);
                album = node.ChildNodes[1].InnerText;
                artist = node.ChildNodes[2].InnerText;
                genre = node.ChildNodes[3].InnerText;
                title = node.ChildNodes[4].InnerText;
                uint.TryParse(node.ChildNodes[5].InnerText, out year);
                filePath = node.ChildNodes[6].InnerText;
                bool.TryParse(node.LastChild.InnerText, out isFavorite);
                var musicItem = new MusicDataItem(uniqueId, album, artist, genre, title, year, filePath)
                {
                    IsFavorite = isFavorite
                };
                MusicDataSource.Music.Add(musicItem);
    I had never thought of pausing on a line then checking the values in the variables, which is what you did.. Thanks again...


    M.K.N

    Thursday, February 12, 2015 6:36 AM