简体   繁体   English

将FlowDocument拆分为C#WPF节

[英]Split flowdocument into sections c# wpf

I have a FlowDocument in a RichTextBox that looks like this: 我在RichTextBox中有一个FlowDocument ,看起来像这样:

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. 

Now I need to split this text up into sections each time INT. 现在,每次INT时,我都需要将此文本分成多个部分。 or EXT. 或EXT。 starts to use later on in the app. 稍后开始在应用中使用。 What's the best way to do this? 最好的方法是什么?

this is how I load the textfile into a richTextBox: 这是我将文本文件加载到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();
            }
      }
}

As FlowDocument syntax is so close to HTML, I would split the solutions up using an HTML parser, see: https://softwarerecs.stackexchange.com/questions/10773/c-library-for-parsing-html . 由于FlowDocument语法非常接近HTML,因此我将使用HTML解析器来拆分解决方案,请参阅: https : //softwarerecs.stackexchange.com/questions/10773/c-library-for-parsing-html You could also experiment with any other XML parser. 您还可以尝试使用任何其他XML解析器。

Whatever you do, don't use regular expressions, as that way leads to madness, as evidenced by the most popular post on StackOverflow: RegEx match open tags except XHTML self-contained tags . 无论您做什么,都不要使用正则表达式,因为这会导致疯狂,正如StackOverflow上最受欢迎的文章所证明的那样: RegEx匹配除XHTML自包含标签之外的其他开放标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM