C# XML getting multible lines in xml document but only keys looked for.

mion shion 241 Reputation points
2021-03-21T19:19:18.577+00:00

ok been working with a big xml file and what im trying to do is parse this xml file and get say,

track id , arti , tite and fnae

and return each item in the xml document that has those values

have tryied following code,

            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\Users\elfen\AppData\Local\elfenliedtopfan5-storeddbs\Database\Database.xml");

            XmlNodeList nodes = doc.SelectNodes("/database/tracks/track");
            foreach (XmlNode node in nodes)
            {
                //string trackID = node.Attributes[0].InnerXml;
                //string Artist = node.Attributes[1].InnerXml; // this works for part of them but each song is diffent and not able to return the fnae as in certin areas it does not fall under the 3rd attribute
                //string Title = node.Attributes[2].InnerXml;

                string testme = node["fnam"].InnerText;

            }

the following one-string test me gives me node not instance of an object error

a rundown of the xml document.
B0qC3hLB

any help would be much appreciated as not quite sure how to do this run through quite a few xml parse methods i know but unable to get the desired result.

Thank you in advance.

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-03-22T01:38:18.39+00:00

    The current method is not the correct way to get attributes.

    You can get the attributes like this:

      string testme = node.Attributes["fnam"].Value;  
    

    79916-12.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.