【发布时间】:2015-05-15 17:42:57
【问题描述】:
我有一个显示 xml 文件内容的文本框。 当查询一个元素/元素时,如果找到,我希望在文本框中突出显示找到的元素和值。
基本上要突出显示,我将调用 textbox1.Select(startIndex, length)。 但我不确定如何检索它的索引和长度。 有人可以帮忙吗?
【问题讨论】:
标签: .net xml linq c#-4.0 textbox
我有一个显示 xml 文件内容的文本框。 当查询一个元素/元素时,如果找到,我希望在文本框中突出显示找到的元素和值。
基本上要突出显示,我将调用 textbox1.Select(startIndex, length)。 但我不确定如何检索它的索引和长度。 有人可以帮忙吗?
【问题讨论】:
标签: .net xml linq c#-4.0 textbox
假设,您将 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);
【讨论】: