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.
精彩评论