Note that there are some explanatory texts on larger screens.

plurals
  1. POworking with MS Word XML
    primarykey
    data
    text
    <p>It's always hard for me to understandably (especially in English which isn't my first language) explain, what my problem is, so I'm sorry in advance for intricacy or excessive triviality ;).</p> <p>What I need to do is to 'parse' Word XML document in a specific way. The document converted to xml has some parts that will be put between some fixed marks like [ ... ] or /* ... */ or whatever and I need them to stay as a one block of text each, while Word from:</p> <pre><code>[SOME_TEXT.SOME_OTHER_TEXT] </code></pre> <p>makes something like:</p> <pre><code>&lt;w:r&gt; &lt;w:rPr&gt;&lt;not relevant /&gt;&lt;/w:rPr&gt; &lt;w:t&gt; [SOME_TEXT. &lt;/w:t&gt; &lt;/w:r&gt; &lt;w:r&gt; &lt;w:rPr&gt;&lt;not relevant /&gt;&lt;/w:rPr&gt; &lt;w:t&gt; SOME_OTHER_TEXT &lt;/w:t&gt; &lt;/w:r&gt; &lt;w:r&gt; &lt;w:rPr&gt;&lt;not relevant /&gt;&lt;/w:rPr&gt; &lt;w:t&gt; ] &lt;/w:t&gt; &lt;/w:r&gt; </code></pre> <p>instead of e.g.:</p> <pre><code>&lt;w:r&gt; &lt;w:rPr&gt;&lt;not relevant /&gt;&lt;/w:rPr&gt; &lt;w:t&gt; [SOME_TEXT.SOME_OTHER_TEXT] &lt;/w:t&gt; &lt;/w:r&gt; </code></pre> <p>I've tried to set Application.Options.StoreRSIDOnSave to false, use common formatting for all the text, switch off the spell checking, etc. but Word still "randomly" splits some strings (especially when they're pasted from somewhere else, not written by hand) - and I cannot tell people, who are going to create those xml docs, to do a hundred other things before they can use their file in my app. So I need to take care of preparing the document by myself. I'm wondering what would be the best and as simple as possible solution to do this - read it through XmlDocument, loop through the nodes and remove them taking care to close the ones that need to be closed and put /* ... */ between clean or do the same but by reading the file as pure text. Or maybe someone has some better idea (like some clever regex ;))? I'll be very grateful for all the help.</p> <p><strong>//edit</strong> I managed to solve the problem. My solution is maybe a little 'lame' but works perfectly ;)</p> <pre><code>Dim MyMarkedString As Boolean = False Dim MyTextOpened As Boolean = False Dim MyFile As String = File.ReadAllText(pFileName) Dim MyFileCopy As String = String.Empty For Each foundPart As Match In Regex.Matches(MyFile, "((&lt;\??/?)(?:[^:\s&gt;]+:)?(\w+).*?(/?\??&gt;))|(?!&lt;)(\[?((?!&lt;).)+\]?)") If (foundPart.Value.Equals("&lt;w:t&gt;") OrElse foundPart.Value.Contains("&lt;w:t ")) AndAlso Not MyMarkedString Then MyTextOpened = True MyFileCopy += foundPart.Value ElseIf (foundPart.Value.Equals("&lt;/w:t&gt;") OrElse foundPart.Value.Contains("&lt;/w:t ")) AndAlso Not MyMarkedString Then MyTextOpened = False MyFileCopy += foundPart.Value ElseIf (foundPart.Value.Equals("&lt;w:t&gt;") OrElse foundPart.Value.Contains("&lt;w:t ")) AndAlso MyMarkedString Then MyTextOpened = True MyFileCopy += "" ElseIf (foundPart.Value.Equals("&lt;/w:t&gt;") OrElse foundPart.Value.Contains("&lt;/w:t ")) AndAlso MyMarkedString Then MyTextOpened = False MyFileCopy += "" Else If MyTextOpened AndAlso Not MyMarkedString Then If foundPart.Value.Contains("[") AndAlso Not foundPart.Value.Contains("]") Then MyMarkedString = True MyFileCopy += foundPart.Value ElseIf MyTextOpened AndAlso MyMarkedString Then If foundPart.Value.Contains("]") AndAlso Not foundPart.Value.Contains("[") Then MyMarkedString = False MyFileCopy += foundPart.Value ElseIf Not MyTextOpened And MyMarkedString Then MyFileCopy += "" Else MyFileCopy += foundPart.Value End If End If Next File.WriteAllText(pCopyName, MyFileCopy) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload