-2

I have an XML file and the structure is roughly like this:

<ScenarioList>
    <Scenario>
        ... various things
    </Scenario>
</ScenarioList>

This is being read into a WPF dialog box. Every time the user selects 'Next' I want to read the next Scenario data into the various fields. Obviously, the same thing goes for the user clicking on the 'Last' button, too.

But the question is: how do i just read in the information from the selected Scenario node?

Let me to try and clarify the question since it appears that it is 'overly broad':

I am familiar with loading and reading entire XML files. I just want to selectively read specific nodes. For example: How can I read just the data for the first Scenario node? Then, depending on user input, read just the data for the second Scenario node? Then, depending on user input, read just the data for the first scenario node? Or just the third scenario node?

In essence, I'm asking since XML doesn't have an 'index' how do I specify which instance of a node to read?

zetar
  • 1,225
  • 2
  • 20
  • 45
  • are you familiar with things like `XPath` or have you even thought to execute a simple google search on `C# how to read / retrieve values from XML File` come on now.. – MethodMan Jun 30 '16 at 14:29
  • 3
    http://stackoverflow.com/questions/2441673/reading-xml-with-xmlreader-in-c-sharp maybe that one helps you – Luca Jun 30 '16 at 14:29
  • 2
    This is too broad, really. Go and look at LINQ to XML and come back when you have a specific question. – Charles Mager Jun 30 '16 at 14:31
  • What I'm asking is how do you read a specific instance of a node without an index? If this was an array I would want to read Scenario[3]. How can I read just the third instance of the ScenarioList/Scenario? – zetar Jun 30 '16 at 14:50

2 Answers2

2

First you need to load the xml into an XmlDocument, then you can select the specific nodes and iterate over them, like this:

XmlDocument xml = new XmlDocument();
xml.LoadXml("your xml"); 

XmlNodeList list = xml.SelectNodes("/ScenarioList/Scenario");
foreach (XmlNode xn in list)...
Crwydryn
  • 840
  • 6
  • 13
  • Yes! This is almost verbatim what I've written. But, how do I read just the desired "/ScenarioList/Scenario" ? Let's say, I want to read the 3rd instance of this "/ScenarioList/Scenario". How would I read just this instance? – zetar Jun 30 '16 at 14:44
  • 1
    Pass in the index list[2] – Crwydryn Jun 30 '16 at 15:21
2

You can traverse to node by using JXPath. If you need to evaluate multiple paths relative to a certain node in the object graph, you might want to create a relative JXPathContext.

First, obtain the pointer for the location that is supposed to be the root the relative context. Then obtain the relative context by calling

 JXPathContext context = JXPathContext.newContext(bean);

 Pointer addressPtr = context.getPointer("/employees[1]/addresses[2]");

 JXPathContext relativeContext = 
              context.getRelativeContext(addressPtr);

 // Evaluate relative path 
 String zipCode = (String)relativeContext.getValue("zipCode");

 // Evaluate absolute path
 String name = (String)relativeContext.getValue("/employees[1]/name");

 // Use the parent axis to locate the employee for the current address
 Double salary = (Double)relativeContext.getValue("../salary");

you can refer in more detail from here

Kaushal
  • 21
  • 3