将flowdocument拆分为c#wpf部分

时间:2015-06-07 20:59:49

标签: c# wpf text richtextbox

FlowDocument中的RichTextBox看起来像这样:

INT. LOCATION - DAY 

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Nulla id risus odio. Donec dictum viverra rutrum. 
Quisque malesuada velit sed augue interdum vehicula. 

                     CHARACTER NAME
                Mauris sit amet nisi varius 
                velit luctus vestibulum tempor egestas augue. 
                Quisque viverra vulputate iaculis. 

Nam sit amet risus at justo feugiat consequat a non ex. Morbi scelerisque libero quam, 
eget imperdiet justo iaculis sed.

EXT. LOCATION - NIGHT

Morbi cursus dictum tempor. Phasellus mattis at massa non porta. 
Etiam ac pellentesque tortor, sit amet facilisis massa. 

                       CHARACTER NAME
                 Aenean viverra convallis dolor, 
                 quis venenatis odio tempus a. 

Sed vel aliquet arcu, in tincidunt est. 

INT. LOCATION - NIGHT

Etiam sit amet tristique sapien. 

现在我需要在每次INT时将此文本拆分为多个部分。或EXT。稍后开始在应用中使用。最好的方法是什么?

这是我如何将文本文件加载到richTextBox:

private void Button_Click(object sender, RoutedEventArgs e)
{

        openFile.InitialDirectory = @"C:\";
        openFile.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*";
        openFile.RestoreDirectory = true;
        openFile.Title = "Select Script";

        if (openFile.ShowDialog() == true)
        {
            string originalfilename = System.IO.Path.GetFullPath(openFile.FileName);

            TextRange range;
            FileStream fStream;

            if (openFile.CheckFileExists)
            {
                range = new TextRange(rtfMain.Document.ContentStart, rtfMain.Document.ContentEnd);
                fStream = new FileStream(originalfilename, System.IO.FileMode.OpenOrCreate);
                range.Load(fStream, DataFormats.Text);
                fStream.Close();
            }
      }
}

1 个答案:

答案 0 :(得分:0)

由于FlowDocument语法非常接近HTML,我会使用HTML解析器拆分解决方案,请参阅:https://softwarerecs.stackexchange.com/questions/10773/c-library-for-parsing-html。您还可以尝试使用任何其他XML解析器。

无论你做什么,都不要使用正则表达式,因为这会导致疯狂,正如StackOverflow上最受欢迎的帖子所证明的那样:RegEx match open tags except XHTML self-contained tags