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 ;).
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:
[SOME_TEXT.SOME_OTHER_TEXT]
makes something like:
<w:r>
<w:rPr><not relevant /></w:rPr>
<w:t>
[SOME_TEXT.
</w:t>
</w:r>
<w:r>
<w:rPr><not relevant /></w:rPr>
<w:t>
SOME_OTHER_TEXT
</w:t>
</w:r>
<w:r>
<w:rPr><not relevant /></w:rPr>
<w:t>
]
</w:t>
</w:r>
instead of e.g.:
<w:r>
<w:rPr><not relevant /></w:rPr>
<w:t>
[SOME_TEXT.SOME_OTHER_TEXT]
</w:t>
</w:r>
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.
//edit I managed to solve the problem. My solution is maybe a little 'lame' but works perfectly ;)
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, "((<\??/?)(?:[^:\s>]+:)?(\w+).*?(/?\??>))|(?!<)(\[?((?!<).)+\]?)")
If (foundPart.Value.Equals("<w:t>") OrElse foundPart.Value.Contains("<w:t ")) AndAlso Not MyMarkedString Then
MyTextOpened = True
MyFileCopy += foundPart.Value
ElseIf (foundPart.Value.Equals("</w:t>") OrElse foundPart.Value.Contains("</w:t ")) AndAlso Not MyMarkedString Then
MyTextOpened = False
MyFileCopy += foundPart.Value
ElseIf (foundPart.Value.Equals("<w:t>") OrElse foundPart.Value.Contains("<w:t ")) AndAlso MyMarkedString Then
MyTextOpened = True
MyFileCopy += ""
ElseIf (foundPart.Value.Equals("</w:t>") OrElse foundPart.Value.Contains("</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)