views:

111

answers:

2

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)
+2  A: 

May i suggest another way: Read the XML as a pure String, remove all XML-Elements and check the resulting string.

Imports System.IO
Imports System.text.RegularExpressions

Dim readFile As String = File.ReadAlltext("yourPathToFile.doc")
readFile = Regex.Replace(readFile, "<[a-zA-Z0-9/:]+>", String.Empty)

For Each foundPart As Match In Regex.Matches(readFile, "\[[a-zA-Z0-9]+\]")
        ' do something here with the things we found'
Next

Some additional things might be needed, f.e. replacing spaces etc.

Edit: Yes, I understand that the RegEx Expression is far from perfect for this...

Edit2: RegEx to remove XML Tags with content

Bobby
Actually I'm trying to come up with some regex that I could use - and hope that's not a dark corner ;)
brovar
I've also found this question, maybe it helps: http://stackoverflow.com/questions/121656/regular-expression-to-remove-xml-tags-and-their-content
Bobby
A: 

What about this SDK?

http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&amp;displaylang=en

Lex Li
Hmm, I'll take a look but it's SDK for OpenXml which is used by Office 2k7 and not 2k3. And I don't know if using something so extended wouldn't be like shooting a sparrow with a cannon ;)
brovar