question

mionshion-3520 avatar image
0 Votes"
mionshion-3520 asked mionshion-3520 commented

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

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.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered mionshion-3520 commented

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.


12.png (47.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your reply i now feel stupid really stupid but thank you ever so much for your reply this has resolved the issue perfectly,

Marked as answer and up voted.

0 Votes 0 ·