Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next node in xml with same element name

Tags:

c#

parsing

xml

I've been working on this problem for a few hours now and I have searched all around with no luck for a solution :(

What I am trying to do is print out names of the nodes, what i have is the amount of nodes that exist so I know how many times to loop but am having the hardest of times retrieving the values

What I have tried:

int num = Convert.ToInt32(queuecount);
var jobs = QueueXML.SelectSingleNode(xpathjobsfilename).InnerText;
PreviousQueue = jobs.ToString();

//foreach(loop < num)
//{
//    if (CurrentQueue == PreviousQueue)
//    {

//    }
//    else
//    {
//        resultsListView.Items.Clear();
//        resultsListView.Items.Add(jobs[num]);
//    }
//    loop++;
//}

foreach (char JobName in jobs.ToString())
{
    if (CurrentQueue == PreviousQueue)
    {
    }
    else
    {
        resultsListView.Items.Clear();
        resultsListView.Items.Add(jobs[num]);
    }
}  

Edit: Example XML

 <jobs>
    <job>
      <timeleft>0:00:00</timeleft>
      <mb>1419.60536003</mb>
      <msgid></msgid>
      <filename>Extended_Final</filename>
      <mbleft>1274.33209419</mbleft>
      <id>nzo_i7qxxq</id>
    </job>
    <job>
      <timeleft>0:00:00</timeleft>
      <mb>9.22459220886</mb>
      <msgid></msgid>
      <filename>Video2</filename>
      <mbleft>9.22459220886</mbleft>
      <id>2m3dv5</id>
    </job>
  </jobs>

I want to retrieve the job details for each individual jobs

like image 873
nGX Avatar asked Dec 22 '25 00:12

nGX


1 Answers

Use this code to loop through your job-nodes.

XmlDocument doc = new Windows.Data.Xml.Dom.XmlDocument();
doc.Load(@"/path/to/xml/file");

foreach (XmlNode job in doc.SelectNodes("/jobs/job"))
{
    string filename = job.SelectSingleNode("filename").InnerText;
    double mbleft = double.Parse(job.SelectSingleNode("mbleft").InnerText);
}

I am not quite sure what you want to do with it. If you want to use that information throughout your program, I'd create a job datatype and parse the XML document to a List<Job>. In any case the above code will enable you to access the information you are after.

like image 149
Jan Avatar answered Dec 23 '25 13:12

Jan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!