开发者

How to get the next and previous node

开发者 https://www.devze.com 2023-01-16 20:23 出处:网络
I am trying to get both the next and previous elements of the current ele开发者_StackOverflow中文版ment

I am trying to get both the next and previous elements of the current ele开发者_StackOverflow中文版ment

Here is the xml

<template id="9">
  <tabs>
    <tab>
      <name>test</name>
      <description />
    </tab>
    <tab>
      <name>test3</name>
      <description />
    </tab>
    <tab>
      <name>test7</name>
      <description />
    </tab>
  </tabs>
  <tabs />
  <tabs />
</template>

The current node is the tab with

test3

here is the code i am using

var doc = XDocument.Parse(q.XMLtext);
var tabs = doc.ElementOrDefault("template").ElementOrDefault("tabs").Elements();
var Current = doc.ElementOrDefault("template")
  .ElementOrDefault("tabs")
  .ElementsOrDefault("tab")
  .ElementsOrDefault("name")
  .Where(x => x.Value == name);
//get the next and previous nodes here


How about this:

var previous = Current.PreviousNode;
var next = Current.NextNode;

The only issue with this is it will return comments and other things that are nodes, but based on your XML this would still work for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消