如何解决c# xmldocument 获取特定节点后的所有兄弟节点
假设我有以下 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
检索所有后续的兄弟节点。例如,
Readable
GetAllSucceedingSiblingNodes 的定义为
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var node = doc.SelectSingleNode(@"//*[@Name='TextboxSearchInjectionEnd']");
var result = GetAllSucceedingSiblingNodes(node);
,
好的 Anu 我有基于你的解决方案:
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;
}
}
再次感谢您!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。