0

I have a XML file with the following structure:

    <deftable>
        <table>
            <job jobname="">
                <incond name="" />
                <outcond name="" />
            </job>
            <job jobname="">
                <incond name="" />
                <outcond name="" />
                <incond name="" />
                <outcond name="" />
            </job>
        </table>
        <table>
            <job jobname="">
                <incond name="" />
                <outcond name="" />
            </job>
            <job jobname="">
                <incond name="" />
                <outcond name="" />
                <incond name="" />
                <outcond name="" />
            </job>
        </table>
    </deftable>

inside the tag deftable i can have multiple tables. in the table tag i can have multiple JOBs, and inside those I have multiple incond and outcond.

I'm trying to obtain the values for jobname, and also the value for the name attribute of incond and outcond.

I have tried several paths to accomplish this. The latest thing I tried was this, but I can't seem to get it to work.

XmlDocument doc = new XmlDocument();
       doc.Load(file);

       XmlNodeList nodes = doc.DocumentElement.SelectNodes("/deftable/table");


       int i = 1;
       foreach (XmlNode node in nodes)
       {              
           Console.WriteLine(node["job"].GetAttribute("jobname") + " -- " + i);
           i++;

           XmlNodeList nodes2 =  doc.DocumentElement.SelectNodes("/deftable/table/job");
           foreach (XmlNode item in nodes2)
           {
               Console.WriteLine(item["incond"].GetAttribute("name"));
           }


       }

Any help is greatly appreciated.

vardiles
  • 71
  • 1
  • 5
  • Have you stepped through it with a debugger? What's the output? What's the expected output? Do you have any clues on what part of the code isn't working? – Mike Christensen Oct 28 '15 at 18:32
  • here's an online xpath tester, you give it a document, and an xpath: http://www.freeformatter.com/xpath-tester.html From that I deduce you need two leading slashes as in `//deftable/table` – GreatAndPowerfulOz Oct 28 '15 at 18:34
  • 1
    [Check this thread](http://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c) for how to go about this. Doesn't look like you are digging down to the child nodes in your loop. – Always Learning Oct 28 '15 at 18:35
  • Thank you all for your help. I managed to access the attributes i wanted using two foreach and reading ChildNodes. The link @AlwaysLearning posted was really helpful, I saw it before, but I didn't understand fully the first time. Thank you. – vardiles Oct 28 '15 at 19:44

2 Answers2

0

I am not sure how much the following snippet is useful for you, but you can try with Linq to Xml for your case as shown below.

 XDocument jobsRoot = XDocument.Load(@"C:\Jobs.xml");
 var jobData= jobsRoot.Descendants()
              .Where(it => it.Name.LocalName == "job")
              .Select(job => new
               {
                  JobName = job.Attribute("jobname").Value,
                  IncondName = job.Descendants().Where(it => it.Name.LocalName == "incond").Select(inc => inc.Attribute("name").Value),
                  OutcondName = job.Descendants().Where(it => it.Name.LocalName == "outcond").Select(inc => inc.Attribute("name").Value)
               });
Siva Gopal
  • 3,474
  • 1
  • 25
  • 22
0

I think the linq to XML implementation isn't as great as XML to Dynamic

It's described on Deserialize XML To Object using Dynamic ( since you mentioned you tried several methods). Otherwhise, the answer above would probably do.

Community
  • 1
  • 1
NicoJuicy
  • 3,435
  • 4
  • 40
  • 66