none
MSXML2 TO SYSTEM.XML in vb.net RRS feed

  • Question

  •  1. Please how do I transform this code to VSTO System.Xml

      Private Function XSLT_Range(ByVal textrange As Word.Range, ByVal xsltfilename As String) As String
            Dim xsldoc As New MSXML2.FreeThreadedDOMDocument60
            Select Case xsltfilename
                Case "imageFormat"
                    xsldoc.loadXML(My.Resources.ImageFormat)
                Case "imageFile"
                    xsldoc.loadXML(My.Resources.ImageFile)
                Case Else
                    Return ""
            End Select
            '       xsldoc.loadXML(My.Resources.ImageFormat) 'load("C:\CBTWordAddIn-New\CBTWordAddIn\XML-Template\ImageFormat.xslt")
            Dim xslt As New MSXML2.XSLTemplate60
            xslt.stylesheet = xsldoc
            Dim xsltProcessor As MSXML2.IXSLProcessor
            xsltProcessor = xslt.createProcessor
            Dim d As New MSXML2.DOMDocument60
            Dim s As String
            d.loadXML(textrange.XML) '!!! Bug in Word 2010 when file is created from a template (Textrange.xml can not be read)
            xsltProcessor.input = d
            xsltProcessor.transform()
            s = xsltProcessor.output

            xsltProcessor = Nothing
            xslt = Nothing
            xsldoc = Nothing
            XSLT_Range = s
        End Function

    2. I try to export this file but the file is empty

    Private Sub Convert2XML()

            Dim fd As Microsoft.Office.Core.FileDialog
            fd = Globals.ThisAddIn.Application.FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogSaveAs)

            fd.FilterIndex = 14
            fd.InitialFileName = FILE_PREFIX & Format(Date.Now, "yyyymmdd") & ".xml"
            If fd.Show <> -1 Then Exit Sub

            Dim sFileStream As FileStream = New FileStream(fd.InitialFileName, FileMode.OpenOrCreate, FileAccess.Write)
            Dim objStream As StreamWriter = New StreamWriter(sFileStream)

            ' Write to the file using StreamWriter class
            objStream.BaseStream.Seek(0, SeekOrigin.End)

    objStream.WriteLine("<?xml version=""1.0""?><quiz>" & vbCr)

            Dim dd, ddans As MSXML2.DOMDocument60
            Dim xmlnod As MSXML2.IXMLDOMNode
            'Dim xmlnodelist As MSXML2.IXMLDOMNodeList
            Dim para As Word.Paragraph, paralookahead As Word.Paragraph
            paralookahead = Nothing

            Dim rac, wac As Integer

            For Each para In Globals.ThisAddIn.Application.ActiveDocument.Paragraphs '?handle each paragraph separately.
                dd = New MSXML2.DOMDocument60

                Select Case para.Style.NameLocal
                    Case STYLE_SHORTANSWERQ
                        With objStream
                            .WriteLine("<question type=""shortchoice"" > ")
                            .WriteLine("<name>")
                            .WriteLine("<text>" + RemoveCR(para.Range.Text) + "</text>")
                            .WriteLine("</name>")
                            .WriteLine("<questiontext format=""html"">")
                            .WriteLine("<text><![CDATA[" + XSLT_Range(para.Range, "imageFormat") + "]]></text>")
                            .WriteLine(XSLT_Range(para.Range, "imagefile"))
                            .WriteLine("</questiontext>")
                            .WriteLine("<penalty>0.1</penalty>")
                            .WriteLine("<hidden>0</hidden>")
                            .WriteLine("<usecase>0</usecase>")
                            paralookahead = para.Next
                            Do While (paralookahead.Style = STYLE_SHORT_ANSWER)
                                .WriteLine("<answer fraction=""100"">")
                                .WriteLine("<text>" + RemoveCR(paralookahead.Range.Text) + "</text>")
                                .WriteLine("<feedback>")
                                .WriteLine("<text></text>")
                                .WriteLine("</feedback>")
                                .WriteLine("</answer>")
                                paralookahead = paralookahead.Next
                                If paralookahead Is Nothing Then Exit Do
                            Loop
                        End With

                   Case STYLE_MULTICHOICEQ
                        With objStream
                            .WriteLine("<question type=""multichoice"">")
                            .WriteLine("<name>")
                            .WriteLine("<text>" + RemoveCR(para.Range.Text) + "</text>")
                            .WriteLine("</name>")
                            .WriteLine("<questiontext format=""html"">")
                            .WriteLine("<text><![CDATA[" + XSLT_Range(para.Range, "imageFormat") + "]]></text>")
                            .WriteLine(XSLT_Range(para.Range, "imageFile"))
                            .WriteLine("</questiontext>")
                            .WriteLine("<penalty>0.1</penalty>")
                            .WriteLine("<hidden>0</hidden>")
                            '                Set ansStream = CreateObject("ADODB.Stream")
                            '                'Initialize the stream
                            '                ansStream.Open
                            '                 'Reset the position and indicate the charactor encoding
                            '                ansStream.Position = 0
                            '                ansStream.Charset = "UTF-8"
                            Dim ansStream As String
                            ansStream = ""
                            ansStream = "<?xml version=""1.0""?> <quiz>" & vbCr & vbCr
                            ansStream = ansStream & "<single>true</single>"
                            paralookahead = para.Next

                            Do While (paralookahead.Style.NameLocal = STYLE_CORRECTANSWER) Or (paralookahead.Style.NameLocal = STYLE_INCORRECTANSWER)
                                If paralookahead.Style.NameLocal = STYLE_CORRECTANSWER Then
                                    ansStream = ansStream & "<answer fraction=""100"">"
                                    ansStream = ansStream & "<text>" + RemoveCR(paralookahead.Range.Text) + "</text>"
                                    ansStream = ansStream & "<feedback>"
                                    ansStream = ansStream & "<text></text>"
                                    ansStream = ansStream & "</feedback>"
                                    ansStream = ansStream & "</answer>"
                                    '                        xmlnod.Attributes.getNamedItem("fraction").text = "100"
                                    rac = rac + 1
                                Else
                                    ansStream = ansStream & "<answer fraction=""0"">"
                                    ansStream = ansStream & "<text>" + RemoveCR(paralookahead.Range.Text) + "</text>"
                                    ansStream = ansStream & "<feedback>"
                                    ansStream = ansStream & "<text></text>"
                                    ansStream = ansStream & "</feedback>"
                                    ansStream = ansStream & "</answer>"
                                    wac = wac + 1
                                End If
                                paralookahead = paralookahead.Next
                                If paralookahead Is Nothing Then Exit Do
                            Loop
                            ansStream = ansStream & "</quiz>"
                            ddans = New MSXML2.DOMDocument60
                            ddans.loadXML(ansStream)
                            If rac > 1 Then
                                ' multiple correct/incorrect answers
                                ddans.documentElement.selectSingleNode("single").text = "false"
                                ' re-looping for setting multi-true-answer fractions
                                For Each mansw In ddans.documentElement.selectNodes("answer")
                                    With mansw.Attributes.getNamedItem("fraction")
                                        If .text = 100 Then .text = Replace(100 / rac, ",", ".")
                                        If .text = 0 Then .text = Replace(-100 / wac, ",", ".")
                                    End With
                                Next mansw
                            End If

               If Not paralookahead Is Nothing Then
                End If

                If dd.xml <> "" Then objStream.WriteLine(dd.xml & vbCr)

                dd = Nothing
            Next para

            objStream.WriteLine("</quiz>")

    end sub

    • Moved by Fred Bao Tuesday, August 12, 2014 6:14 AM
    Monday, August 11, 2014 9:04 AM

All replies

  • Hello,

    I move it to VSTO forum for getting better help.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 12, 2014 6:13 AM
  • Hello,

    Did you try to debug the code? Do you get any exception in the code?

    Also I noticed the following line of code:

    For Each para In Globals.ThisAddIn.Application.ActiveDocument.Paragraphs 

    It looks like you don't release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Word object when you have finished using it. Set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article. It describes Outlook, but the same can be applied to any Office application.

    Tuesday, August 12, 2014 2:48 PM