tags:

views:

47

answers:

0

Hi everyone,

My request is quite specific so I will do my best to explain it properly. If you have any questions please ask!

Basically I have built a web page that reads an English XML file and displays its contents on the left of the page as text, divided up by each C-DATA tag it finds. It also then loads the equivalent translated version of the XML file and displays the C-DATA text on the right, but this time in editable text boxes.

For example the page displays:

English - German


Hello - Hallo


Something - Etwas


etc

This works fine and the code is already in place to overwrite the translated XML file with any changes you make in the editable text boxes.

My issue comes when the English XML file is updated to add in new lines (adding another XML Node with C-DATA text inside it) the translated XML files data appears out of sync. Basically for every new line you add in the English XML file a blank text editor will appear at the bottom of the translated text.

For example:

English XML File

<item>
<name><![CDATA[Some text]]></name>
<value><![CDATA[Some more text]]></value>
<value><![CDATA[Some more new text]]></value>
</item>

<item>
<name><![CDATA[Some new text]]></name>
<value><![CDATA[Some more new text]]></value>
</item>

<itemnew>
<name><![CDATA[Some text]]></name>
<value><![CDATA[Some more text]]></value>
</itemnew>

Translated XML File

<item>
<name><![CDATA[Some text]]></name>
<value><![CDATA[Some more text]]></value>
</item>

<item>
<name><![CDATA[Some new text]]></name>
<value><![CDATA[Some more new text]]></value>
</item>

So as you can hopefully see loading in these two XML files will result in 7 sections of English text on the left, and 4 sections of Translated text on the right, with 3 blank sections following underneath.

I want to know if it is possible for me to accurately compare these two XML files so I can keep the data appearing in sync, and just add in a blank text editor where a new English line has been added.

The code I am currently using to read in the two XML files is below (this is cut down to just show the main/relevant code):

    Dim MyDT As New DataTable
    Dim MyRow As DataRow

    MyDT.Columns.Add(New DataColumn("xmlValue", GetType(String)))
    MyDT.Columns.Add(New DataColumn("xmlValue_Translated", GetType(String)))

    ' ----- START ENGLISH XML ----- '
    Dim counterText_English As Integer = 0

    Dim document_English As XmlDocument = New XmlDocument

    Dim file_English As String = Current.Server.MapPath("EnglishXML.xml")

    document_English.Load(file_English)

    Dim reader_English As New XmlTextReader(file_English)

    Dim xmlValue As String = ""

    While reader_English.Read

        If reader_English.NodeType = XmlNodeType.Text Or reader_English.NodeType = XmlNodeType.CDATA Then

            MyRow = MyDT.NewRow()

            xmlValue = reader_English.Value

            If xmlValue = Nothing Then xmlValue = ""

            MyRow("xmlValue") = xmlValue
            MyRow("xmlValue_Translated") = ""

            MyDT.Rows.Add(MyRow)

        End If

    End While

    reader_English.Close()
    ' ----- END ENGLISH XML ----- '

    ' ----- START TRANSLATED XML ----- '
    If File.Exists(Current.Server.MapPath("TranslatedXML.xml")) Then

        Dim document_Translated As XmlDocument = New XmlDocument

        Dim file_Translated As String = Current.Server.MapPath("TranslatedXML.xml")

        document_Translated.Load(file_Translated)

        Dim reader_Translated As New XmlTextReader(file_Translated)

        Dim counter_Translated As Integer = 0

        While reader_Translated.Read

            If reader_Translated.NodeType = XmlNodeType.Text Or reader_Translated.NodeType = XmlNodeType.CDATA Then

                xmlValue = reader_Translated.Value

                If xmlValue = Nothing Then xmlValue = ""

                MyDT.Rows(counter_Translated).Item("xmlValue_Translated") = xmlValue

                counter_Translated += 1

            End If

        End While

        reader_Translated.Close()

    End If
    ' ----- END TRANSLATED XML ----- '

I have previously tried saving extra data into the data table for each line of text (such as parent node name, counter of how many nodes, etc) and then comparing that with the translated XML file to work out if the line exists or not. So far I haven't found a solution that works across the board. As you can see from my examples there are a number of ways of adding new text into the English XML file and I would ideally like to account for them all, or as many as possible.

I apologise if this isn't clear enough, please let me know if you have any questions. To me this is more a question of logic than necessarily VB code, but anyone who can get their head round it better than me I will be eternally grateful for their help.

Thank you.