Linq Query 然后在文本框中突出显示 xml 文本

Linq Query then highlight xml text in texbox

我有一个显示 xml 文件内容的文本框。 当查询 element/elements 时,如果找到,我希望在文本框中突出显示找到的元素和值。

基本上是为了突出显示,我将调用 textbox1.Select(startIndex, length)。 但我不确定如何检索它的索引和长度。 有人可以帮忙吗?

A​​ssumig,您将 xml 保存为文本框中的文本,例如

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to> Tove</to>
 <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

并且您想使用 textBox.Select 函数突出显示 <from>Jani</from>,您可以试试这个:

        const string searchString = "<from>Jani</from>";
        var searchStringIndex = textBox1.Text.IndexOf(searchString, StringComparison.Ordinal);
        if(searchStringIndex > -1)
             textBox1.Text.Select(searchStringIndex, searchString.Length);