选定的标记元素未写入新文档

时间:2019-04-11 18:04:00

标签: vb.net

此代码有效,现在却无效。我不知道为什么。该代码应该执行的操作是将所有内容读入并将其粘贴到新文件中。

现在,无论开始/停止标记如何,它都将抓取所有文本

我尝试将insideBlock更改为true或false。我还尝试过更改源文档,以确保注释标记正确。

这是我的代码

    Dim startMark As String = "<!--#start#-->"
    Dim stopMark As String = "<!--#stop#-->"
    searchDir = txtDirectory.Text
    Prefix = txtBxUnique.Text
For Each singleFile In allFiles
    If File.Exists(singleFile.FullName) Then
        Dim fileName = singleFile.FullName
        Debug.Print("file name : " & fileName)
        ' A backup first    
        Dim backup As String = fileName & ".bak"
        File.Copy(fileName, backup, True)

        ' Load lines from the source file in memory
        Dim lines() As String = File.ReadAllLines(backup)

        ' Now re-create the source file and start writing lines inside a block
        Dim insideBlock As Boolean = False
        Using sw As StreamWriter = File.CreateText(backup)
            For Each line As String In lines
                If line = startMark Then
                    ' start writing at the line below
                    insideBlock = True
                ElseIf line = stopMark Then
                    ' Stop writing
                    insideBlock = False
                ElseIf insideBlock = True Then
                    ' Write the current line in the block
                    sw.WriteLine(line)
                End If
            Next
        End Using
    End If
Next

此处的源数据

<!--Arbortext, Inc., 1988-2010, v.4002-->
<!DOCTYPE DOC PUBLIC "-//USA-DOD//DTD 38784STD-BV7//EN" [
<!ENTITY graphic3-16_dgcs_file_window SYSTEM  Graphics\3-16_dgcs_file_window.cgm" NDATA cgm>
<!ENTITY graphic19000_2 SYSTEM "Graphics\19000_2.cgm" NDATA cgm> ]>
<?Pub UDT _bookmark _target>
<?Pub EntList alpha bull copy rArr sect trade deg>
<?Pub Inc>
<doc service="af" docid="TO 1Q-1(M)B-2-2-12-1" docstat="formal" verstatpg="ver" cycle="2" chglevel="1">
<front numcols="1">
<idinfo>
<tmidno></tmidno>
<chgnum></chgnum>
<chgdate></chgdate>
<chghistory>
<chginfo>
<chgtxt></chgtxt>
<date></date></chginfo></chghistory>
<doctype></doctype>
<maintlvl></maintlvl>
<!--#start#-->
<prtitle>
<subject>
Trying to get this code to work</subject></prtitle>
<mfr></mfr>
<contractno>A12345</contractno>
<contractno>B12</contractno>
<!--#start#-->
<line></line>
<contractno>Contract No</contractno>
<supersed></supersed>
<discl>Discipline</discl>
<distrib>
<emphasis type="u"></emphasis></distrib>
<expcont></expcont>
<destr>Destruction</destr>
<authnot></authnot>
<pubdate></pubdate></idinfo>
<lep>
<lepcontents autobuild="1"></lep>
<contents autobuild="1">
<illuslist autobuild="1">
<tablelist autobuild="1">
<foreword>
<!--#start#-->
<para0 verstatus="ver">
<title></title>
<para>
This text should be in the new file</para></para0>
<para0 verstatus="ver">
<title></title>
<para>
<!--#stop#-->
<acronym>
<def></def>
<term></term></acronym></para></para0>
<?Pub Caret -2></foreword>
<safesum>
<para0 verdate="5/2/16" verstatus="ver">
<title>
</title>
<para>
</para>
</para0></safesum></front>
<body numcols="1">
<chapter id="chap1">
<title>Chapter 1</title>
<para0 verstatus="ver">
<title>Paragraph 1</title>
<para></para></para0></chapter>
<chapter id="chap2">
<title>Chapter 2</title>
<section id="thoery_of_operation_section">
<title></title>
<para0 verstatus="ver">
<title>Paragraph 2</title>
<para>
<!--#start#-->
<change level="1" change="delete">
<emphasis type="u" color="blue">
<xref xrefid="fig2-1">Figure 1 Smelly Fish</emphasis></change>
<!--#stop#-->
</para></para0></section>
<section>
<title></title>
<para0>
<title></title></para0></section></chapter>
</body>
</doc>

结果应该只是标记之间的元素。

<prtitle>
<subject>
Trying to get this code to work</subject></prtitle>
<mfr></mfr>
<contractno>A12345</contractno>
<contractno>B12</contractno>
<para0 verstatus="ver">
<title>Para0 Title</title>
<para>
This text should be in the new file</para></para0>
<para0 verstatus="ver">
<title></title>
<para>
<acronym>
<def>definition</def>
<term>Look here</term></acronym></para>
<change level="1" change="delete">
<emphasis type="u" color="blue">
<xref xrefid="fig2-1">Figure 1 Smelly Fish</emphasis></change>

感谢您的帮助, 最高

0 个答案:

没有答案