xPath获取具有特定值的元素的同级值

时间:2022-03-28 09:13:31

Given this XML:

鉴于这种XML:

<InitResponse>
  <LottoToken>908ec70b308adf10d04db1478ef9b01b</LottoToken>
  <GameInfoList>
    <GameInfo>
      <Draw>
        <gameId>L649</gameId>
        <draw>3035</draw>
      </Draw>
    </GameInfo>
    <GameInfo>
      <Draw>
        <gameId>BC49</gameId>
        <draw>2199</draw>
      </Draw>
    </GameInfo>
  </GameInfoList>
</InitResponse>

I need to get the draw number based on a specific gameId. For example if I specify gameID L649 I need to get 3035.

我需要根据一个特定的gameId来获取绘制数字。例如,如果我指定gameID L649,我需要得到3035。

The following works in several online evaluators, but not in C#. It says it cannot find it. Suggestions?

下面的方法适用于几个在线评估程序,但不适用c#。它说它找不到它。建议吗?

/InitResponse/GameInfoList/GameInfo/Draw/draw[preceding-sibling::gameId='L649']

C# Code I've tried:

我已经试过c#代码:

XmlNode node = xmlDoc.SelectSingleNode("/InitResponse/GameInfoList/GameInfo/Draw/draw[preceding-sibling::gameId='L649']");

... where xmlDoc is an xmlDocument object loaded with the xml. the node variable ends up with a null value which seems to indicate there was no match found.

…xmlDoc是加载了xml的xmlDocument对象。节点变量以一个空值结束,这个空值似乎表明没有找到匹配。

2 个解决方案

#1


5  

Here is xpath (with Linq)

这里是xpath(带有Linq)

var xdoc = XDocument.Load(path_to_xml);
string xpath = "/InitResponse/GameInfoList/GameInfo/Draw[gameId='L649']/draw";
var draw = xdoc.XPathSelectElement(xpath);
if (draw != null) // check if draw with gameId found in xml
    value = (int)draw;

Also you can use pure Linq to Xml (but in this case xpath looks more compact):

还可以使用纯Linq到Xml(但在本例中xpath看起来更紧凑):

var draw = xdoc.Descendants("GameInfo")
               .SelectMany(g => g.Elements("Draw"))
               .SingleOrDefault(d => (string)d.Element("gameId") == "L649");
if (draw != null)
    value = (int)draw.Element("draw");

#2


1  

Using XmlDocument

使用XmlDocument

I didn't saw something wrong in your XPath statement, look on the following:

我在您的XPath语句中没有看到什么问题,请查看以下内容:

(So i guess there is something else that is wrong)

(所以我想还有其他的错误)

XmlDocument myDoc = new XmlDocument();

String str = @"<InitResponse>
                 <LottoToken>908ec70b308adf10d04db1478ef9b01b</LottoToken>
                     <GameInfoList>
                         <GameInfo>
                             <Draw>
                               <gameId>L649</gameId>
                               <draw>3035</draw>
                              /Draw>
                            </GameInfo>
                            <GameInfo>
                              <Draw>
                                <gameId>BC49</gameId>
                                <draw>2199</draw>
                              </Draw>
                            </GameInfo>
                          </GameInfoList>
                        </InitResponse>";

            myDoc.LoadXml(str);

            XmlNode node =
                myDoc.SelectSingleNode("/InitResponse/GameInfoList/GameInfo/Draw/draw[preceding-sibling::gameId='L649']");

The node which returns from the result is: 3035

从结果返回的节点是:3035

Note: your first note have to be <InitResponse> otherwise it will returns null

注意:您的第一个音符必须是 ,否则它将返回null

#1


5  

Here is xpath (with Linq)

这里是xpath(带有Linq)

var xdoc = XDocument.Load(path_to_xml);
string xpath = "/InitResponse/GameInfoList/GameInfo/Draw[gameId='L649']/draw";
var draw = xdoc.XPathSelectElement(xpath);
if (draw != null) // check if draw with gameId found in xml
    value = (int)draw;

Also you can use pure Linq to Xml (but in this case xpath looks more compact):

还可以使用纯Linq到Xml(但在本例中xpath看起来更紧凑):

var draw = xdoc.Descendants("GameInfo")
               .SelectMany(g => g.Elements("Draw"))
               .SingleOrDefault(d => (string)d.Element("gameId") == "L649");
if (draw != null)
    value = (int)draw.Element("draw");

#2


1  

Using XmlDocument

使用XmlDocument

I didn't saw something wrong in your XPath statement, look on the following:

我在您的XPath语句中没有看到什么问题,请查看以下内容:

(So i guess there is something else that is wrong)

(所以我想还有其他的错误)

XmlDocument myDoc = new XmlDocument();

String str = @"<InitResponse>
                 <LottoToken>908ec70b308adf10d04db1478ef9b01b</LottoToken>
                     <GameInfoList>
                         <GameInfo>
                             <Draw>
                               <gameId>L649</gameId>
                               <draw>3035</draw>
                              /Draw>
                            </GameInfo>
                            <GameInfo>
                              <Draw>
                                <gameId>BC49</gameId>
                                <draw>2199</draw>
                              </Draw>
                            </GameInfo>
                          </GameInfoList>
                        </InitResponse>";

            myDoc.LoadXml(str);

            XmlNode node =
                myDoc.SelectSingleNode("/InitResponse/GameInfoList/GameInfo/Draw/draw[preceding-sibling::gameId='L649']");

The node which returns from the result is: 3035

从结果返回的节点是:3035

Note: your first note have to be <InitResponse> otherwise it will returns null

注意:您的第一个音符必须是 ,否则它将返回null