INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help with modify XML Data

Help with modify XML Data

(OP)
Hello people I need to a code that makes following things:

I have got this XML code,

I had help making to create line breaks in the file. I want following:, if you look, it's several sections, starting tag and end

that can be x number of <file> * </ file> x times. the modified tags will be inside of and tags: example in the file I sent: (it can be any line but the structure is always the same)

line 11:

<inContextExact segments="60" words="55" characters="755" placeables="3" tags="0" />
replace 55 with 0

result:

<inContextExact segments="60" words="0" characters="755" placeables="3" tags="0" />

next tag is:

line:14

<crossFileRepeated segments="2" words="20" characters="0" placeables="0" tags="0" />

Cut two words "20" and replace with 0

result:

line:14<crossFileRepeated segments="2" words="0" characters="0" placeables="0" tags="0" />

add the value 20 to the tag words="34"

line 15:

<repeated segments="17" words="34" characters="293" placeables="2" tags="0" />
result:


line 15:

<repeated segments="17" words="54" characters="293" placeables="2" tags="0" />
Here is my code I got so far. (VBA) if you want to rewrite the code in any other lang is it OK! as long the result is fine.
(the only thing my VBA code do its look for >< replace with >\n\r<) because the XML file is 1 line.


Sub XMLSolve()
Dim FSO As Object '//FileSystemObject
Dim ts(1) As Object '//TextStream
Dim s As String, t As String
Dim FileContents As String

'---------------------------------------------------------
Const SEARCH_FOR As String = "><"
Const REPLACE_WITH As String = ">" & vbCrLf & "<"
'----------------------------------------------

s = "<123><456><789>"
s = Replace(s, SEARCH_FOR, REPLACE_WITH)

On Error GoTo ErrHandler:
s = Application.GetOpenFilename()
If s <> "False" Then
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(s) Then

'//Get File Contents
t = FSO.GetParentFolderName(s) & "\" & Replace(FSO.GetTempName(), ".tmp", ".xml")
Name s As t
Set ts(0) = FSO.OpenTextFile(t, 1, False, -2) '//For reading, use default encoding
FileContents = ts(0).ReadAll
ts(0).Close
Set ts(0) = Nothing

'//Make replacement
FileContents = Replace(FileContents, SEARCH_FOR, REPLACE_WITH)



MsgBox



'//Write new file contents
Set ts(1) = FSO.OpenTextFile(s, 2, True, -2) '//For writing, use default encoding
ts(1).Write (FileContents)
ts(1).Close
Set ts(1) = Nothing

'//Delete Temp file if all actions succeeded
FSO.DeleteFile (t)

End If
End If

'//Check that all files are closed
My_Exit:
If Not ts(0) Is Nothing Then
ts(0).Close
End If
If Not ts(1) Is Nothing Then
ts(1).Close
End If
Set FSO = Nothing
Exit Sub

ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume My_Exit
End Sub

Here is the XML file:

<task name="analyse">
<taskInfo taskId="21a09311-ade3-4e9a-af21-d13be8b7ba45" runAt="2015-05-20 13:48:50" runTime="5 minutes, 53 seconds">
<project name="13955 - HMI Volvo Truck PA15" number="e20d51c0-71dc-4572-8f9b-4c150bf35222" />
<language lcid="1031" name="German (Germany)" />
<tm name="ENG-DEU_en-GB_de-DE.sdltm" />
<settings reportInternalFuzzyLeverage="yes" reportLockedSegments="no" reportCrossFileRepetitions="yes" minimumMatchScore="70" searchMode="bestWins" missingFormattingPenalty="1" differentFormattingPenalty="1" multipleTranslationsPenalty="1" autoLocalizationPenalty="0" textReplacementPenalty="0" />
</taskInfo>
<file name="VT MAIN TRACK_PA15_Default_DE-DE_20150520_102527.xlf.sdlxliff" guid="111f9ba6-82f6-45fb-ac49-8bf6cf57c169">
<analyse>
<perfect segments="0" words="0" characters="0" placeables="0" tags="0" />
<inContextExact segments="60" words="55" characters="755" placeables="3" tags="0" /> ' Replace the Value word="55" with "0"
<exact segments="114" words="334" characters="1687" placeables="14" tags="3" />
<locked segments="0" words="0" characters="0" placeables="0" tags="0" />
<crossFileRepeated segments="2" words="20" characters="0" placeables="0" tags="0" /> 'Cut the value words="20" replace with 0
<repeated segments="17" words="34" characters="293" placeables="2" tags="0" /> 'add the value to current value 20 to 34 so the new value is words="54"
<total segments="449" words="1462" characters="7630" placeables="66" tags="24" />
<new segments="126" words="434" characters="2384" placeables="18" tags="5" />
<fuzzy min="75" max="84" segments="25" words="108" characters="528" placeables="6" tags="3" />
<fuzzy min="85" max="94" segments="23" words="92" characters="454" placeables="7" tags="4" />
<fuzzy min="95" max="99" segments="77" words="260" characters="1318" placeables="13" tags="6" />
<internalFuzzy min="75" max="84" segments="3" words="16" characters="100" placeables="2" tags="2" />
<internalFuzzy min="85" max="94" segments="4" words="25" characters="111" placeables="1" tags="1" />
<internalFuzzy min="95" max="99" segments="0" words="0" characters="0" placeables="0" tags="0" />
</analyse>
</file>
<file name="VT MAIN TRACK_PA15_Default_DE-DE_20150523_254796.xlf.sdlxliff" guid="111f9ba6-82f6-45fb-ac49-8bf6cf57c169">
<analyse>
<perfect segments="0" words="0" characters="0" placeables="0" tags="0" />
<inContextExact segments="60" words="67" characters="755" placeables="3" tags="0" /> ' Replace the Value word="67" with "0"
<exact segments="114" words="334" characters="1687" placeables="14" tags="3" />
<locked segments="0" words="0" characters="0" placeables="0" tags="0" />
<crossFileRepeated segments="2" words="35" characters="0" placeables="0" tags="0" /> 'Cut the value words="35" replace with 0
<repeated segments="17" words="54" characters="293" placeables="2" tags="0" /> 'add the value to current value 35 to 54 so the new value is words="89"
<total segments="449" words="1462" characters="7630" placeables="66" tags="24" />
<new segments="126" words="434" characters="2384" placeables="18" tags="5" />
<fuzzy min="75" max="84" segments="25" words="108" characters="528" placeables="6" tags="3" />
<fuzzy min="85" max="94" segments="23" words="92" characters="454" placeables="7" tags="4" />
<fuzzy min="95" max="99" segments="77" words="260" characters="1318" placeables="13" tags="6" />
<internalFuzzy min="75" max="84" segments="3" words="16" characters="100" placeables="2" tags="2" />
<internalFuzzy min="85" max="94" segments="4" words="25" characters="111" placeables="1" tags="1" />
<internalFuzzy min="95" max="99" segments="0" words="0" characters="0" placeables="0" tags="0" />
</analyse>
</file>
<batchTotal>
<analyse>
<perfect segments="0" words="0" characters="0" placeables="0" tags="0" />
<inContextExact segments="60" words="139" characters="755" placeables="3" tags="0" />
<exact segments="114" words="334" characters="1687" placeables="14" tags="3" />
<locked segments="0" words="0" characters="0" placeables="0" tags="0" />
<crossFileRepeated segments="0" words="0" characters="0" placeables="0" tags="0" />
<repeated segments="17" words="54" characters="293" placeables="2" tags="0" />
<total segments="449" words="1462" characters="7630" placeables="66" tags="24" />
<new segments="126" words="434" characters="2384" placeables="18" tags="5" />
<fuzzy min="75" max="84" segments="25" words="108" characters="528" placeables="6" tags="3" />
<fuzzy min="85" max="94" segments="23" words="92" characters="454" placeables="7" tags="4" />
<fuzzy min="95" max="99" segments="77" words="260" characters="1318" placeables="13" tags="6" />
<internalFuzzy min="75" max="84" segments="3" words="16" characters="100" placeables="2" tags="2" />
<internalFuzzy min="85" max="94" segments="4" words="25" characters="111" placeables="1" tags="1" />
<internalFuzzy min="95" max="99" segments="0" words="0" characters="0" placeables="0" tags="0" />
</analyse>
</batchTotal>
</task>



Could someone help me with a batchscript /script

Thank you in advance




RE: Help with modify XML Data

You still didn't tell what exactly is your problem, what do you want to achieve?

Please describe exactly what it is you want to achieve. If I were you, I wouldn't try to parse this as a text file - too much pain.
Use XmlDOMDocument60 instead.

BTW: If you you don't terribly mind switching to VB.Net then you might think about joining the SDL Developer Community. There you can get the SDKs amongst which is the Project Automation API, which has a AnalysisStatistics class that will give you access to all data from the analysis.
winky smile

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
Well Sorry If I wasn't clear enough but if you look on the XML a part of the file:




here is the Example original:

<file name="VT MAIN TRACK_PA15_Default_DE-DE_20150520_102527.xlf.sdlxliff" guid="111f9ba6-82f6-45fb-ac49-8bf6cf57c169">
<analyse>
<perfect segments="0" words="0" characters="0" placeables="0" tags="0" />
<inContextExact segments="60" words="55" characters="755" placeables="3" tags="0" /> ' Replace the Value word="55" with "0"
<exact segments="114" words="334" characters="1687" placeables="14" tags="3" />
<locked segments="0" words="0" characters="0" placeables="0" tags="0" />
<crossFileRepeated segments="2" words="20" characters="0" placeables="0" tags="0" /> 'Cut the value words="20" replace with 0
<repeated segments="17" words="34" characters="293" placeables="2" tags="0" /> 'add the value to current value 20 to 34 so the new value is words="54"
<total segments="449" words="1462" characters="7630" placeables="66" tags="24" />
<new segments="126" words="434" characters="2384" placeables="18" tags="5" />
<fuzzy min="75" max="84" segments="25" words="108" characters="528" placeables="6" tags="3" />
<fuzzy min="85" max="94" segments="23" words="92" characters="454" placeables="7" tags="4" />
<fuzzy min="95" max="99" segments="77" words="260" characters="1318" placeables="13" tags="6" />
<internalFuzzy min="75" max="84" segments="3" words="16" characters="100" placeables="2" tags="2" />
<internalFuzzy min="85" max="94" segments="4" words="25" characters="111" placeables="1" tags="1" />
<internalFuzzy min="95" max="99" segments="0" words="0" characters="0" placeables="0" tags="0" />
</analyse>
</file>


here is result I want


<file name="VT MAIN TRACK_PA15_Default_DE-DE_20150520_102527.xlf.sdlxliff" guid="111f9ba6-82f6-45fb-ac49-8bf6cf57c169">
<analyse>
<perfect segments="0" words="0" characters="0" placeables="0" tags="0" />
<inContextExact segments="60" words="0" characters="755" placeables="3" tags="0" />
<exact segments="114" words="334" characters="1687" placeables="14" tags="3" />
<locked segments="0" words="0" characters="0" placeables="0" tags="0" />
<crossFileRepeated segments="2" words="0" characters="0" placeables="0" tags="0" />
<repeated segments="17" words="54" characters="293" placeables="2" tags="0" />
<total segments="449" words="1462" characters="7630" placeables="66" tags="24" />
<new segments="126" words="434" characters="2384" placeables="18" tags="5" />
<fuzzy min="75" max="84" segments="25" words="108" characters="528" placeables="6" tags="3" />
<fuzzy min="85" max="94" segments="23" words="92" characters="454" placeables="7" tags="4" />
<fuzzy min="95" max="99" segments="77" words="260" characters="1318" placeables="13" tags="6" />
<internalFuzzy min="75" max="84" segments="3" words="16" characters="100" placeables="2" tags="2" />
<internalFuzzy min="85" max="94" segments="4" words="25" characters="111" placeables="1" tags="1" />
<internalFuzzy min="95" max="99" segments="0" words="0" characters="0" placeables="0" tags="0" />
</analyse>
</file>

RE: Help with modify XML Data

OK, just to clarify:
You want to set all inContextExact word values to 0, and summarize crossFileRepeated words and repated words in the repeated node and set crossFileRepeated also to 0?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
I want to:
<inContextExact segments="60" words=" 55" characters="755" placeables="3" tags="0" />
result:
<inContextExact segments="60" words=" 0" characters="755" placeables="3" tags="0" />


and
<crossFileRepeated segments="2" words=" 20" characters="0" placeables="0" tags="0" /> 'Cut the value words=" 20" replace with 0
result:


<repeated segments="17" words=" 34" characters="293" placeables="2" tags="0" /> 'add the value to current value 20 to 34 so the new value is words=" 54"

result:
<repeated segments="17" words=" 54" characters="293" placeables="2" tags="0" /> 'add the value to current value 20 to 34 so the new value is words=" 54"

RE: Help with modify XML Data

Hi Elmas,

look at the follwing little script, it does exactly what you want (need to replace the hardcoded test paths to the file of course):

CODE

Dim analyse
Dim exactContexts
Dim subnode
Dim repeated
Dim realRepeated
Dim tmpValue

Set analyse = CreateObject("Msxml2.DOMDocument.6.0")
analyse.Load "C:\00_Projekte_temp\analyse.xml"

Set exactContexts = analyse.SelectNodes("//inContextExact")

For i = 0 To exactContexts.Length - 1
    Set subnode = exactContexts(i)
    For Each att In subnode.Attributes
        If att.Name = "words" Then
            att.Value = "0"
            Exit For
        End If
    Next att
Next i

Set repeated = analyse.SelectNodes("//crossFileRepeated")

For i = 0 To repeated.Length - 1
    Set subnode = repeated(i)
    For Each att In subnode.Attributes
        If att.Name = "words" Then
            tmpValue = att.Value
            att.Value = "0"
            Exit For
        End If
    Next att
    
    Set realRepeated = subnode.NextSibling
    For Each att In realRepeated.Attributes
        If att.Name = "words" Then
            att.Value = Val(att.Value) + Val(tmpValue)
            Exit For
        End If
    Next att
Next i

analyse.Save "C:\00_Projekte_temp\analyse2.xml" 

That OK?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
do I need to add any reference plugin?

strange the code generates an empty file?
Thank you in advance

RE: Help with modify XML Data

The above is the naked VBScript, if you run this from say VBA, you will need a reference to Microsoft Scripting Runtime.
It should work without explicit reference to Microsoft XML 6.0.

Once you give the correct path to the xml (instead of "C:\00_Projekte_temp\analyse.xml"), it should produce the correct output.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
Hmm I activated the references
also hardcoded my location of file
analyse.Load "C:\test\example.xml"


Microsoft XML, v6.0
Microsoft Scripting Runtime


xxxx

analyse.Save "C:\test\analyse2.xml"



but I get runtime error '424':
Object required.



when I go debug:

It marks

For Each att In realRepeated.Attributes


any ideas?

Thank you in advance

RE: Help with modify XML Data

Where do you run this from?
VB6? VBA? Word? Excel? Version?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

P.S: I ran that code from Word VBA and it worked fine. I have set references to Microsoft XML 6.0, Microsoft Scripting Runtime and Microsoft VBScript Regular Expressions 5.5

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
oh jesus I run it from Excel? but is it any differences?
otherwise is it possible to make the code work in microsoft word?
same problem in Microsoft word I run microsoft word 2010 small & business



Thank you in advance

RE: Help with modify XML Data

Very strage!
I just pasted the code as is into Excel, no references set and it ran without problems.
Have you changed anything, added or removed any line?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

Hmmm.. Version? Operating System?
Maybe you need to install MS xml core services?
http://www.microsoft.com/en-us/download/details.as...

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
Hi again

I run windows 7 Pro,
Service pack 1
I also Have installed the Microsoft Core XML Services (MSXML) 6.0 Service Pack 1
I run microsoft office 2013

RE: Help with modify XML Data

(OP)
Have you got teamviewer?

Thank you in advance

RE: Help with modify XML Data

I think I have a hunch what is going on.

CODE

Set realRepeated = subnode.NextSibling 
This is assuming that the node <repeated> always directly follows the node <crossFileRepeated>, which it does in a normal Studio analysis.
Have you changed anything about the XML? added comments or anything?

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
I havent changed anything in the xml,

Where will I put the new code(line/place) snipet? "Set realRepeated = subnode.NextSibling "

thank you in advance

RE: Help with modify XML Data

(OP)
It actually works now! Awesome!!!

how do I make the code adds a dialogwindow and I select a folder then the script takes the whole folder and loop through the xml files?

thank you in advance

RE: Help with modify XML Data

Not possible in VBA (at least not without breaking ones neck about it).
VBA User forms do not have the folder and file browser controls available.

In VBA I usually simply use an inputbox and have the user simply paste the path there. Or save it in a Word .doc or .docm.
If you open that .doc(m) from the root folder you wish to process, you could use "ActiveDocument.Path".

Do you have VB6 or VB.Net Express available? You could then create a full standalone .exe.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
Yes I have got VB6 also Visual studio 2010

Could you help me out?
that would be a dream! :D

I have added your code to a button and work but two changes I would like to have one button for select folder I see the text under where the location is set.
also of course batch process.

RE: Help with modify XML Data

I won't write the entire code for you, else you don't learn from it! winky smile

Simply create a new VB6 project with a form. In the toolbox list you will find a DriveListBox, DirListBox and FileListBox.
They are for choosing a drive, a folder and a file respectively.
Then put this code in your form:

CODE

Private Sub Drive1_Change()
Dir1.Path=Drive1.Drive
End Sub

Private Sub Dir1_Change()
File1.Path=Dir1.Path
End Sub 

If you now run the form, you'll find that when you change the drive letter, the directory list and file list will automatically snychronize to the new path.
If you put some kind of "Start" button on your form you can then iterate through all files in the chosen Path like this:

CODE

For i = 0 to Form1.File1.ListCount -1
  Fil = Form1.File1.Path & "\" & Form1.File1.List(i)
'...do your processing per file here
Next i 

Good luck!

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
I actually run Visual studio express 2010.

I guess its almost the same thing may some updates in there I guess :)

But Thank you very much! so far! KING

RE: Help with modify XML Data

(OP)
Hi again MakeitSo,

I should need osme help I run Visual studio 2010,
I am not sure what to replace the DriveListBox, DirListBox and FileListBox with.

Could you give me a hint?

what about the earlier code?

CODE

Private Sub Drive1_Change()
Dir1.Path=Drive1.Drive
End Sub

Private Sub Dir1_Change()
File1.Path=Dir1.Path
End Sub  

Thank you in advance.

RE: Help with modify XML Data

In VB.Net you have FileOpenDialog and OpenFolderDialog. I prefer the FileOpenDialog because the folder one is junk.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
Dam I don't get it work hmm.
have you got a tutorial or something for this?

I googled loads I think the problem is when I got so little experience earlier I can't combine the things I found on the web.


Thank you in advance.

RE: Help with modify XML Data

How about:

CODE

'...
Dim allAnalyses

allAnalyses=Directory.EnumerateFiles(mySelFile, "*.xml") 

And then iterate through the result:

CODE

Dim allAnalyses
    allAnalyses=Directory.EnumerateFiles(mySelFile, "*.xml")
    For Each analyse As String In allAnalyses
        
    Next 

From here on however this is rather something for a new thread in forum796: Visual Basic(Microsoft) -VB.NET

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
I finally got this code!!

and its working but it someonething the code complains on:

CODE

Public Class SdlStudioChanger

    Private Sub BT_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Browse.Click

        ' Set Folder PATH default
        FolderBrowserDialog1.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments

        ' Show the new folder button
        FolderBrowserDialog1.ShowNewFolderButton = True

        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' Get the full path to the file that selected by the user.
            Dim mySelFile As String = FolderBrowserDialog1.SelectedPath

            ' Displays the full path of the file selected by the user in the box (TextBox)
            TB_chemin_dossier.Text = mySelFile

            'Displays the folder name (only) selected, the user"Subtlety, use the" IO.Path.GetFileName "on the path to a folder
            'To get the name of the target folder.
            'While "IO.Path.GetDirectoryName" would have shown you the folder path CONTAINING file
            'Targeted by the specified path as a parameter
            'MsgBox("Du har valt: " & IO.Path.GetFileName(mySelFile))
            For Each filename As String In IO.Directory.GetFiles(mySelFile, "*.xml")

                Dim analyse
                Dim exactContexts
                Dim subnode
                Dim repeated
                Dim realRepeated
                Dim tmpValue

                analyse = CreateObject("Msxml2.DOMDocument.6.0")
                analyse.Load(filename)

                exactContexts = analyse.SelectNodes("//inContextExact")

                For i = 0 To exactContexts.Length - 1
                    subnode = exactContexts(i)
                    For Each att In subnode.Attributes
                        If att.Name = "words" Then
                            att.Value = "0"
                            Exit For
                        End If
                    Next att
                Next i

                repeated = analyse.SelectNodes("//crossFileRepeated")

                For i = 0 To repeated.Length - 1
                    subnode = repeated(i)
                    For Each att In subnode.Attributes
                        If att.Name = "words" Then
                            tmpValue = att.Value
                            att.Value = "0"
                            Exit For
                        End If
                    Next att

                    realRepeated = subnode.NextSibling
                    For Each att In realRepeated.Attributes
                        If att.Name = "words" Then
                            att.Value = Val(att.Value) + Val(tmpValue)
                            Exit For
                        End If
                    Next att
                Next i

                analyse.Save(filename)
                MsgBox("nu är filen: " & filename & " ändrade")
            Next
        Else
            'if the user has not selected a folder, it is a warning
            MsgBox("Ingen fil vald", MsgBoxStyle.Exclamation, "Inga markerade mappar")
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

End Class 


RE: Help with modify XML Data

Two things:
1) this is just a warning. In this case you can ignore it.
2) far more serious is this line:

CODE

For Each filename As String In IO.Directory.GetFiles(mySelFile, "*.xml")
'...
analyse.Save(filename) 
You should never iterate over the collection you are (sort of) changing. Apart from possible side-effects a copy of your array is probably being re-queried with every Next.
Assign the result of GetFiles to a string array variable and use that array in your for each instead.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
I did this code is it ok?

CODE

Dim tmpValue As String = "" 

RE: Help with modify XML Data

Yeah, that's fine.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
I have a little question I am almost really done with my code and yours haha!

How do I add an alert that tells me how many files are modified? when its finished?


also if there is no xml files in the folder i select then msgbox no xml files in the folder.
Thank you in advance

RE: Help with modify XML Data

1.) Two simple integer variables should suffice for that.

CODE

Dim contextModified As Integer = 0
Dim repeatModified as Integer = 0 
then later, before you set the words to "0", just add a little checker before it:

CODE

If att.Value <> 0 Then contextModified = contextModified+1
att.Value = "0" 

And the same if block for "repeatedModified". Now you have the amount of modified nodes in these two variables each.

The other is as simple as:

CODE

If Not Directory.GetFiles(mySelFile,"*.xml").Any() Then
        MsgBox("No files to process.")
    Else 
        'do your stuff
    End If 

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Help with modify XML Data

(OP)
new code

CODE

Public Class SdlStudioChanger

    Private Sub BT_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Browse.Click

        ' Set Folder PATH default
        FolderBrowserDialog1.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments

        ' Show the new folder button
        FolderBrowserDialog1.ShowNewFolderButton = True

        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' Get the full path to the file that selected by the user.
            Dim mySelFile As String = FolderBrowserDialog1.SelectedPath




            ' Displays the full path of the file selected by the user in the box (TextBox)
            Tb_FilePath.Text = mySelFile

            'Displays the folder name (only) selected, the user"Subtlety, use the" IO.Path.GetFileName "on the path to a folder
            'To get the name of the target folder.
            'While "IO.Path.GetDirectoryName" would have shown you the folder path CONTAINING file
            'Targeted by the specified path as a parameter
            'MsgBox("Du har valt: " & IO.Path.GetFileName(mySelFile))
            If Not IO.Directory.GetFiles(mySelFile, "*.xml").Any() Then
                MsgBox("No files to process.")
            Else


                For Each filename As String In IO.Directory.GetFiles(mySelFile, "*.xml")

                    Dim analyse
                    Dim exactContexts
                    Dim subnode
                    Dim repeated
                    Dim realRepeated
                    Dim tmpValue As String = ""
                    Dim contextModified As Integer = 0
                    Dim repeatModified As Integer = 0


                    analyse = CreateObject("Msxml2.DOMDocument.6.0")
                    analyse.Load(filename)

                    exactContexts = analyse.SelectNodes("//inContextExact")

                    For i = 0 To exactContexts.Length - 1
                        subnode = exactContexts(i)
                        For Each att In subnode.Attributes
                            If att.Name = "words" Then
                                att.Value = "0"
                                Exit For
                            End If
                        Next att
                    Next i

                    repeated = analyse.SelectNodes("//crossFileRepeated")

                    For i = 0 To repeated.Length - 1
                        subnode = repeated(i)
                        For Each att In subnode.Attributes
                            If att.Name = "words" Then
                                tmpValue = att.Value
                                If att.Value <> 0 Then contextModified = contextModified + 1
                                att.Value = "0"
                                Exit For
                            End If
                        Next att

                        realRepeated = subnode.NextSibling
                        For Each att In realRepeated.Attributes
                            If att.Value <> 0 Then repeatModified = repeatModified + 1
                            If att.Name = "words" Then
                                att.Value = Val(att.Value) + Val(tmpValue)
                                Exit For
                            End If
                        Next att
                    Next i

                    'msgbox'
                    analyse.Save(filename)

                Next

            End If

            'if the user has not selected a folder, it is a warning
            MsgBox("Ingen fil vald", MsgBoxStyle.Exclamation, "Inga markerade mappar")
        End If



    End Sub 

I think I have put an if somewhere wrong cause I get a msg (ingen fil vald) all the time. hmm

RE: Help with modify XML Data

(OP)
Solved partly by using


CODE

Dim intcount As Integer = Nothing




            For Each filename As String In IO.Directory.GetFiles(mySelFile, "*.xml")


       
                intcount = intcount + 1



 MsgBox("det är (" + intcount.ToString + ") filer som är modiferade") 

RE: Help with modify XML Data

(OP)
I am finished now need to find a tutorial how to compile it?

I guess I get all errors while I press ctrl + F5
cause I have done the settings wrong.





error:

CODE

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at SDL_studio_analys_Changer.SdlStudioChanger.BT_Browse_Click(Object sender, EventArgs e) in C:\Users\daniel.elmnas.TT\Desktop\iw_selection_dossier\iw_selection_dossier\Interface.vb:line 75
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
SDL studio analys Changer
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/daniel.elmnas.TT/Desktop/iw_selection_dossier/iw_selection_dossier/bin/Debug/SDL%20studio%20analys%20Changer.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 12.0.51209.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34238 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34251 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34245 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
CustomMarshalers
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box. 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close