假设我有以下xml:
<Report>
<Tablix></Tablix>
<Textbox Name="TextboxSearchInjection"></Textbox>
<Textbox></Textbox>
<Tablix></Tablix>
<Textbox></Textbox>
<Textbox Name="TextboxSearchInjectionEnd">
<Height>1</Height>
</Textbox>
<Tablix>
<Height>1</Height>
</Tablix>
<Textbox>
<Height>2</Height>
</Textbox>
<Textbox></Textbox>
<Tablix>
<Height>1</Height>
</Tablix>
<Textbox>
<Height>3</Height>
</Textbox>
</Report>
如何获得<Textbox Name="TextboxSearchInjectionEnd">
之后的所有兄弟节点和使用c#中的XmlDocument在高度更新值?所以最后我将收到节点列表,如Textbox和Tablix,但从节点<Textbox Name="TextboxSearchInjectionEnd">
开始。我以前不想要。
您可以使用NextSibling
检索所有后续兄弟节点。例如,
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var node = doc.SelectSingleNode(@"//*[@Name='TextboxSearchInjectionEnd']");
var result = GetAllSucceedingSiblingNodes(node);
其中GetAllSucceedingSiblingNodes定义为
IEnumerable<XmlNode> GetAllSucceedingSiblingNodes (XmlNode node)
{
var currentNode = node.NextSibling; // Use currentNode = node if you want to include searched node
while(currentNode!=null)
{
yield return currentNode;
currentNode = currentNode.NextSibling;
}
}
好的,我有一个基于你的解决方案:
private static IEnumerable<XmlNode> GetAllSucceedingSiblingNodes(XmlNode node, XmlNamespaceManager nsmgr)
{
var currentNode = node;//.NextSibling; // Use currentNode = node if you want to include searched node
while (currentNode != null &&
currentNode.Attributes["Name"].Value != "TextboxSearchInjection")
{
yield return currentNode;
currentNode = currentNode.NextSibling;
}
}
再一次非常感谢!