如何循环遍历XML文件中的节点[重复]

时间:2022-04-12 12:51:36

This question already has an answer here:

这个问题在这里已有答案:

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

我有一个XML文件,结构大致如下:

<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.

这被读入WPF对话框。每次用户选择“下一步”时,我都希望将下一个场景数据读入各个字段。显然,用户同样点击“上一步”按钮也是如此。

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

但问题是:我如何从所选的Scenario节点中读取信息?

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?

我熟悉加载和读取整个XML文件。我只想有选择地读取特定节点。例如:如何只读取第一个Scenario节点的数据?然后,根据用户输入,只读取第二个Scenario节点的数据?然后,根据用户输入,只读取第一个场景节点的数据?或者只是第三个场景节点?

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

本质上,我问,因为XML没有'索引'如何指定要读取的节点的哪个实例?

2 个解决方案

#1


2  

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

首先,您需要将xml加载到XmlDocument中,然后您可以选择特定节点并迭代它们,如下所示:

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

XmlNodeList list = xml.SelectNodes("/ScenarioList/Scenario");
foreach (XmlNode xn in list)...

#2


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.

您可以使用JXPath遍历节点。如果需要评估相对于对象图中某个节点的多个路径,则可能需要创建相对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

你可以从这里更详细地参考

#1


2  

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

首先,您需要将xml加载到XmlDocument中,然后您可以选择特定节点并迭代它们,如下所示:

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

XmlNodeList list = xml.SelectNodes("/ScenarioList/Scenario");
foreach (XmlNode xn in list)...

#2


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.

您可以使用JXPath遍历节点。如果需要评估相对于对象图中某个节点的多个路径,则可能需要创建相对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

你可以从这里更详细地参考