1

次の XML を検討してください。

<SomeRoot>
  <SomeElement>
    <SomeThing>25</SomeThing>
    <SomeOther>Cat</SomeOther>
  </SomeElement>
  <SomeElement>
    <SomeThing>46</SomeThing>
    <SomeOther>Dog</SomeOther>
  </SomeElement>
  <SomeElement>
    <SomeThing>83</SomeThing>
    <SomeOther>Bat</SomeOther>
  </SomeElement>
  <SomethingElse>Unrelated to the SomeElements above</SomethingElse>
</SomeRoot>

したいselect SomeThing where SomeOther = 'Cat'。次の C# コードは、null 参照例外をスローします。

xmlDoc = new XmlDocument();
this.path = path;
// Path is passed elsewhere

Console.WriteLine(xmlDoc.SelectSingleNode("/SomeRoot/SomeElement/SomeThing[../SomeOther='Cat']").InnerText);

ここで使用する正しい XPath 構文は何ですか?

4

3 に答える 3

2

あなたは負荷を逃しています

var xmlDoc = new XmlDocument();
xmlDoc.Load(path);
Console.WriteLine(xmlDoc.SelectSingleNode("/SomeRoot/SomeElement/SomeThing[../SomeOther='Cat']").InnerText);
于 2012-08-31T08:13:34.007 に答える
1

これが私がしたことであり、それは機能します。

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(@"<SomeRoot><SomeElement><SomeThing>25</SomeThing><SomeOther>Cat</SomeOther></SomeElement></SomeRoot>");

var x = xDoc.SelectSingleNode("/SomeRoot/SomeElement/SomeThing[../SomeOther= 'Cat']").InnerText;
于 2012-08-31T08:10:41.517 に答える
1

逆軸の使用を避けることができます

/*/SomeElement[SomeOther='Cat']/SomeThing
于 2012-08-31T12:53:42.793 に答える