【问题标题】:Linq Query then highlight xml text in texboxLinq Query 然后在文本框中突出显示 xml 文本
【发布时间】:2015-05-15 17:42:57
【问题描述】:

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

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

【问题讨论】:

    标签: .net xml linq c#-4.0 textbox


    【解决方案1】:

    假设,您将 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 函数突出显示&lt;from&gt;Jani&lt;/from&gt;,你可以试试这个:

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

    【讨论】:

    • 这是一种方式,从文本框中搜索。我在想是否有一种方法可以在我搜索 xml 节点时,如果找到,然后将位置直接突出显示到文本框。而不是找到元素,然后转到文本框内容并再次搜索它。我现在会为你的答案投票。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多