c# xmldocument 获取特定节点之后的所有兄弟节点

c# xmldocument get all siblings nodes after a specific one

假设我有以下 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>

如何在c#中使用XmlDocument获取<Textbox Name="TextboxSearchInjectionEnd">之后的所有兄弟节点并更新Height中的值? 所以最后我会收到像 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;
            }
        }

再来一次,非常感谢!