Im trying to excersize on substring but its not working good
-
Friday, April 27, 2012 5:24 AM
The code is:
private void test() { int countFiles = 0; byte[] a; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; index = 0; w = new StreamWriter(@"c:\test.txt"); while (true) { index = f.IndexOf(startTag, index); string test = f.Substring(index , startTagWidth); if (index == -1) { break; }The startTag is T256= the endTag should be one qouta "
I need to find to get anything between T256 and "
For example if i have T256="Hi Mose" so i need to get only Hi Mose
For now what im getting is T256=
What should i do next ? ( the substring is for testing so far but not what i need in the final result )
danieli
All Replies
-
Friday, April 27, 2012 5:39 AM
Hi Danieli,
I found some old code in my library. It is working fair with your requirement.
private void Form1_Load(object sender, EventArgs e) { string data = "T256=\"Hi Mose\""; string startTag = "T256=\""; string endTag = "\""; string result = GetStringBetween(data, startTag, endTag); } public string GetStringBetween(string data, string StartKey, string EndKey) { if (String.IsNullOrEmpty(data) || String.IsNullOrEmpty(StartKey) || String.IsNullOrEmpty(EndKey)) return String.Empty; if (!data.Contains(StartKey) || !data.Contains(EndKey)) return String.Empty; int ix_start = data.IndexOf(StartKey); int ix_end = data.IndexOf(EndKey, ix_start + StartKey.Length); int ValueStart = ix_start + StartKey.Length; string ret = data.Substring(ValueStart, ix_end - ix_start - StartKey.Length); return ret; }
Please try it.
Resolving n Evolving in C# (http://jeanpaulva.com)
-
Friday, April 27, 2012 5:45 AM
Jean its working but i dont have the data. the string data variable. It was example.
In fact i need to loop over a file and get everything in any place between the startTag and the endTag it dosent matter wich data there is i need to get everything between each place in the file between the startTag and the endTag using While(true) or something like that reading line by line.
danieli
-
Friday, April 27, 2012 5:46 AM
Hi danieli,
The syntax of substring is
public string Substring(int startIndex, int length)
So for the required output you must modify the code in the while loop as
startTagIndex = f.IndexOf(startTag, index); endTagIndex = f.IndexOf(endTag, index); //Get the index of the sting to be displayed stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed stringLength = endTagIndex - stringIndex; string test = f.Substring(stringIndex , stringLength);
Hope your doubt is clered. -
Friday, April 27, 2012 7:07 AM
Hi danieli,
The syntax of substring is
public string Substring(int startIndex, int length)
So for the required output you must modify the code in the while loop as
startTagIndex = f.IndexOf(startTag, index); endTagIndex = f.IndexOf(endTag, index); //Get the index of the sting to be displayed stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed stringLength = endTagIndex - stringIndex; string test = f.Substring(stringIndex , stringLength);
Hope your doubt is clered.I tried it but getting error here is my code:
private void testing() { string startTag = "T256="; string endTag = "\""; int startTagIndex = f.IndexOf(startTag, index); int endTagIndex = f.IndexOf(endTag, index); int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; //Get the index of the sting to be displayed int stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed int stringLength = endTagIndex - stringIndex; index = 0; w = new StreamWriter(@"D:\Testing.txt"); while (true) { string test = f.Substring(stringIndex, stringLength); if (index == -1) { break; }On the string test...line im getting error: Length cannot be less than zero
stringLength is -39934628 and it cant be minus
danieli
-
Friday, April 27, 2012 7:15 AM
danieli,
you should at least provide the code of all used variables. Nowhere in your code you have defined f,w and index. It is just a guessing for us what f should contain.
Hannes
If you have got questions about this, just ask.
In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/ -
Friday, April 27, 2012 7:19 AM
Hi chocolade,
I prefer you open the file and read line by line.
The following code will provide a hint.
foreach (string data in File.ReadAllLines("c:\\file.txt")) { string startTag = "T256=\""; string endTag = "\""; string result = GetStringBetween(data, startTag, endTag); }
Resolving n Evolving in C# (http://jeanpaulva.com)
-
Friday, April 27, 2012 7:23 AM
The variable index is int and i start it as 0 index = 0;
The variable w is StreamWriter in the top Form1 level i did: StreamWriter w; Then i create a new instance in this function im not using yet the w to write anything when i will have the working loop with the working results i will write the results to the Testing.txt file.
The variable f is just a string in the Form1 top level i did String f;
Then in the constructor i did:
r = new StreamReader(@"D:\New folder (24)\000004aa.xml"); f = r.ReadToEnd();
danieli
-
Friday, April 27, 2012 7:24 AM
The actual file im working on that i want to get the text from between the startTag and endTag is the 000004aa.xml
danieli
-
Friday, April 27, 2012 7:28 AM
Hi danieli,
The syntax of substring is
public string Substring(int startIndex, int length)
So for the required output you must modify the code in the while loop as
startTagIndex = f.IndexOf(startTag, index); endTagIndex = f.IndexOf(endTag, index); //Get the index of the sting to be displayed stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed stringLength = endTagIndex - stringIndex; string test = f.Substring(stringIndex , stringLength);
Hope your doubt is clered.I tried it but getting error here is my code:
private void testing() { string startTag = "T256="; string endTag = "\""; int startTagIndex = f.IndexOf(startTag, index); int endTagIndex = f.IndexOf(endTag, index); int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; //Get the index of the sting to be displayed int stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed int stringLength = endTagIndex - stringIndex; index = 0; w = new StreamWriter(@"D:\Testing.txt"); while (true) { string test = f.Substring(stringIndex, stringLength); if (index == -1) { break; }On the string test...line im getting error: Length cannot be less than zero
stringLength is -39934628 and it cant be minus
danieli
I meant for those sentences to be in the while loop.
What is f? From where are you getting the string?
These information may help us to give you a more accurate solution.
-
Friday, April 27, 2012 7:33 AM
Hi danieli,
The syntax of substring is
public string Substring(int startIndex, int length)
So for the required output you must modify the code in the while loop as
startTagIndex = f.IndexOf(startTag, index); endTagIndex = f.IndexOf(endTag, index); //Get the index of the sting to be displayed stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed stringLength = endTagIndex - stringIndex; string test = f.Substring(stringIndex , stringLength);
Hope your doubt is clered.I tried it but getting error here is my code:
private void testing() { string startTag = "T256="; string endTag = "\""; int startTagIndex = f.IndexOf(startTag, index); int endTagIndex = f.IndexOf(endTag, index); int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; //Get the index of the sting to be displayed int stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed int stringLength = endTagIndex - stringIndex; index = 0; w = new StreamWriter(@"D:\Testing.txt"); while (true) { string test = f.Substring(stringIndex, stringLength); if (index == -1) { break; }On the string test...line im getting error: Length cannot be less than zero
stringLength is -39934628 and it cant be minus
danieli
I meant for those sentences to be in the while loop.
What is f? From where are you getting the string?
These information may help us to give you a more accurate solution.
f is empty. f is just a variable i created in the Form1 top level before the constructor like:
string f;
I used it as:
The variable f is just a string in the Form1 top level i did String f;
Then in the constructor i did:
r = new StreamReader(@"D:\New folder (24)\000004aa.xml"); f = r.ReadToEnd();
danieli
-
Friday, April 27, 2012 7:47 AM
When ever you are using \ (backslashes) in your text, can come to some errors, sooner or later. Because \ is a reserved char for escape sequences.
I would try to not using it - if only is this possible - replace them with some other chars, like some uniqe character(s) - examplke <text>, or <<text>>, something that cannot be found in the text-
But if you wanna stick to it, you can still use IndexOf, and LastIndexOf methods:
string input = @"T256=\some text in between\not ending text"; int start = input.IndexOf('\\'); int end = input.LastIndexOf('\\'); string newString = input.Substring(start + 1, (end - start) - 1); //output: some text in between
Note, how I use @ simbol infront of an example text, if I wouldnt use it, there will be a compile time error, since baqckslashes inside a vlaue are escape characters, and not used like ONLY-A-CHAR. If I put @ infront, all characters are taken as characters only - even backslashes are now not escapre characters any longer.
So be careful using them.
Mitja
-
Friday, April 27, 2012 7:50 AM
Hi danieli,
The syntax of substring is
public string Substring(int startIndex, int length)
So for the required output you must modify the code in the while loop as
startTagIndex = f.IndexOf(startTag, index); endTagIndex = f.IndexOf(endTag, index); //Get the index of the sting to be displayed stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed stringLength = endTagIndex - stringIndex; string test = f.Substring(stringIndex , stringLength);
Hope your doubt is clered.I tried it but getting error here is my code:
private void testing() { string startTag = "T256="; string endTag = "\""; int startTagIndex = f.IndexOf(startTag, index); int endTagIndex = f.IndexOf(endTag, index); int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; //Get the index of the sting to be displayed int stringIndex = startTagIndex + startTagWidth; //Get the length of the sting to be displayed int stringLength = endTagIndex - stringIndex; index = 0; w = new StreamWriter(@"D:\Testing.txt"); while (true) { string test = f.Substring(stringIndex, stringLength); if (index == -1) { break; }On the string test...line im getting error: Length cannot be less than zero
stringLength is -39934628 and it cant be minus
danieli
I meant for those sentences to be in the while loop.
What is f? From where are you getting the string?
These information may help us to give you a more accurate solution.
f is empty. f is just a variable i created in the Form1 top level before the constructor like:
string f;
I used it as:
The variable f is just a string in the Form1 top level i did String f;
Then in the constructor i did:
r = new StreamReader(@"D:\New folder (24)\000004aa.xml"); f = r.ReadToEnd();
danieli
hi danieli,
The following code should work.
private static void test() { int countFiles = 0; byte[] a; string startTag = "T256=\""; string endTag = "\""; int index = 0; int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; int fileLength = f.Length; w = new StreamWriter(@"c:\test.txt"); while (true) { if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; string test = f.Substring(stringIndex, stringLength); } }Just give it a try.
Hope it works.
Ferdin Raphael
-
Friday, April 27, 2012 8:06 AM
Ferdin the string test is all the time ""
I used breakpoint on this line: string test = f.Substring(stringIndex, stringLength);
test is ""
And stringLength is 0 all the time
And the loop never end im waiting now almost over 5 minutes or so.
danieli
-
Friday, April 27, 2012 8:35 AM
This is a link in SkyDrive for the file im using on:
https://skydrive.live.com/redir.aspx?cid=3b8a7d9f66ff985b&resid=3B8A7D9F66FF985B!160&parid=3B8A7D9F66FF985B!129&authkey=!ADNrJ_iA0XjUfy0
Its xml file.
If i load open it with notepad i can search(find) the tag: T256 for example:
</T260>
<T254 T18="378572" T52="377032" T19="19">
<T255 T12="Unbenannt" T13="1" T14="0" T55="82" T257="" T256="">
<T258 T21="F" T13="0" T19="18" T36="F"/>But thats a problem since T256="" so there is nothing there.
But in this example from the file:
/T255>
<T255 T12="Unbenannt" T13="24" T14="0" T55="-1" T257="" T256="Hinter Tonis Hütte">
<T258 T21="F" T13="0" T19="18" T36="F"/>There is text wich i need to get the text is: Hinter Tonis Hütte
Or in this case wich is also a problem:
</T255>
<T255 T12="Unnamed" T13="153226" T14="0" T55="143" T257="data\speech\de\ch3\cutscenes\C_CH03_05\C_CH03_05_i02\C_CH03_05_i02_014.ogg#s#050#00149#" T256="Ihr habt den Mann gehört.<pa>">I need to get only: Ihr habt den Mann gehört.
So there are many cases and im not sure startTag as T256 is good idea or maybe there should be some IF to check sometimes.
In any case i need to get all this texts if there is text between T256 and the endTag wich im not sure what it should be as endTag in all cases.
danieli
-
Friday, April 27, 2012 9:09 AM
danieli,
you can use this method:
private List<String> ParseFile(String filepath, String startArgument, String endArgument) { int iStartIndex = 0; int iEndIndex = 0; int isearchStartLength = startArgument.Length; String[] lines = System.IO.File.ReadAllLines(filepath); List<String> foundValues = new List<string>(); foreach (String line in lines) { if (!String.IsNullOrEmpty(line)) { iStartIndex = line.IndexOf(startArgument, iStartIndex); if (iStartIndex != -1) { iEndIndex = line.IndexOf(endArgument, iStartIndex + isearchStartLength + 1); if (iEndIndex != -1) { foundValues.Add(line.Substring(iStartIndex + isearchStartLength, iEndIndex - iStartIndex - isearchStartLength)); } } else { iStartIndex = -1; } } } return foundValues; }you can call it like:
List<String> foundValues = ParseFile(@"D:\New folder (24)\000004aa.xml", @"T256=""", @"""");
Hannes
If you have got questions about this, just ask.
In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/ -
Friday, April 27, 2012 9:38 AM
This function is working by Ferdin but i need few more things:
The working code:
private void test() { int countFiles = 0; byte[] a; string startTag = "T256=\""; string endTag = "\""; int index = 0; int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; int fileLength = f.Length; w = new StreamWriter(@"d:\testing.txt"); while (true) { if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength == 0) { } else { string test = f.Substring(stringIndex, stringLength); listBox1.Items.Add(test); } } }1. In som e places the string that im getting are like: Hello and bye.<pa> I need to get the strings in this places without the <pa> its only in some places not in all the places.
2. I need to make a reverse. In this function im getting retriving the text strings between two tags. Im doing some manipulations on the text then i need to read from the text file line by line the text and put each line back to the original place in the big file.
I have a streamWriter i didnt use it but im using now so its writing all the results to a text file.
I need to read from this text file line by line and each line to put in the big file back before i did the changes to replace it.
For example im using now the function by Ferdin lets say the first line is at number 23 and there i have T256="hello and bye".....
Now i have in the new text file hello and bye and i change the text file to: bye and goodbye now i want to read this bye and goodbye and put it in the original file instead of hello and bye so if i will read the original big file again in line 23 it will be : T256="bye and goodbye"......
And the same thing for each line and line...to read one line from the new text file: testing.txt and put it in the place in the big file.
danieli
-
Friday, April 27, 2012 10:27 AM
Hi danieli,
In terms of the first issue,
i think some of the values have <pa> within the quotes. Thats why you are getting the output as such.
Just check the data in the data file.for the second issue, you can follow these steps.
Read your xml into a string variable f just as before.
use a loop to read your text file linewise
Place the following code in the loopif (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + str + f.Substring(stringIndex + stringLength); }(Here str is the line you have read from the text file assuming you have written each value in different line)
write the value of f back into your xml file.
Hope this helps.
Ferdin Raphael
- Edited by Ferdin Raphael Friday, April 27, 2012 10:29 AM additional info
-
Friday, April 27, 2012 12:59 PM
Hi chocolade ,
check this code using this you can get all between your atart tag and endtag.
private void test()
{
string startTag = "T256=\"";
string endTag = "\"";
StreamReader SR = new StreamReader("C:\\text.txt");
string r = SR.ReadToEnd();
if (r.Contains(startTag) && r.Contains(endTag))
{
int i = r.IndexOf(startTag);
int j = r.IndexOf(endTag);
string result = r.Substring(i, j - i);
}
}
-
Saturday, April 28, 2012 5:37 AM
Hi chocolade ,
check this code using this you can get all between your atart tag and endtag.
private void test()
{
string startTag = "T256=\"";
string endTag = "\"";
StreamReader SR = new StreamReader("C:\\text.txt");
string r = SR.ReadToEnd();
if (r.Contains(startTag) && r.Contains(endTag))
{
int i = r.IndexOf(startTag);
int j = r.IndexOf(endTag);
string result = r.Substring(i, j - i);
}
}- Proposed As Answer by Rohit Binjola Saturday, April 28, 2012 3:54 PM
-
Sunday, April 29, 2012 4:47 AM
Hi danieli,
In terms of the first issue,
i think some of the values have <pa> within the quotes. Thats why you are getting the output as such.
Just check the data in the data file.for the second issue, you can follow these steps.
Read your xml into a string variable f just as before.
use a loop to read your text file linewise
Place the following code in the loopif (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + str + f.Substring(stringIndex + stringLength); }(Here str is the line you have read from the text file assuming you have written each value in different line)
write the value of f back into your xml file.
Hope this helps.
Ferdin Raphael
Ferding this is how i tried to do it im not sure if it will work.
In the Form1 top i have two StreamReader variables: r and h
Then in the constructor im doing: r = new StreamReader(@"D:\New folder (24)\000004aa.xml"); to read the big file
f = r.ReadToEnd();
Then in the constructor i also read the text file: h = new StreamReader(@"d:\test.txt"); This is the file i need to read line by line and add back to the xml.
Then the code it self to add back the text is:
private void addingTranslatedDeponiaFiles() { StreamWriter w = new StreamWriter(@"D:\New folder (24)\000004aa.xml"); w.AutoFlush = true; int currentLength; int currentIndex; int lastIndex = 0; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; //int endTagWidth = endTag.Length; int index = 0; while ((line = h.ReadLine()) != null) { if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + str + f.Substring(stringIndex + stringLength); } } }First of all the variable str does not exist.
Second this function the code im not sure if its right.
And last im not sure if im doing good the readings in the constructor and where to add the writing in the function.
Could you modify/fix my code including in the constructor ?
Thanks.
danieli
-
Sunday, April 29, 2012 7:13 AM
Hi danieli,
In your code, str can be replaced with line.
The function is correct. It works perfectly in my computer.
Following is the code I tried on my system:
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace ConsoleApplication1 { class Program { StreamReader r, h; string line, f; public Program() { r = new StreamReader(@"D:\New folder (24)\000004aa.xml"); f = r.ReadToEnd(); r.Close(); } static void Main(string[] args) { Program p = new Program(); p.addingTranslatedDeponiaFiles(); } private void addingTranslatedDeponiaFiles() { //int currentLength; //int currentIndex; //int lastIndex = 0; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; //int endTagWidth = endTag.Length; int index = 0; h = new StreamReader(@"d:\test.txt"); while ((line = h.ReadLine()) != null) { if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + line + f.Substring(stringIndex + stringLength); } } h.Close(); StreamWriter w = new StreamWriter(@"D:\New folder (24)\000004aa.xml"); w.AutoFlush = true; w.Write(f); w.Close(); } } }
I have read the text file inside the function, because it is good programming practice to close the stream as soon as the work is done.
If the stream is left open, then the file will remain inaccessible elsewhere.
Hope this helps.
Ferdin Raphael
-
Sunday, April 29, 2012 1:03 PM
Im using now the code to add the text back and its working now almost 40-50minutes already.
Is it logic that its taking so long time ? The big file size is 43mb the small file the text file is about 900kb
danieli
-
Sunday, April 29, 2012 1:17 PM
Its working now for almost an hour and yet it didnt get to to the line: w.Write(f);
I used a breakpoint on the StreamWriter in the bottom of this function and still it didnt get there yet.
danieli
-
Sunday, April 29, 2012 2:29 PM
Just tried it. After adding back the text to the big xml i found that the text is not in the right places.
So the problem is either with the text extracting or the text adding.
I think its with the
addingTranslatedDeponiaFiles()Function but im not sure what the problem is if at all.
I know that the text is not in place in the big xml file thats for sure.
danieli
-
Sunday, April 29, 2012 3:54 PM
When i tried a small xml file, it worked fine without any problem.
Can you send me the xml and txt files via my mail id (ferdinraphael@gmail.com) if possible.
I shall try it on my system and find out what is wrong.
Ferdin Raphael
- Edited by Ferdin Raphael Sunday, April 29, 2012 3:58 PM
-
Sunday, April 29, 2012 4:28 PM
Ferdin i just sent you the two files.
One is the big xml file 47mb wich is original i didnt change yet anything.
And the text file wich is about 900kb the one i need to put back in the big xml in the right places and replace.
In fact i translated the small text file to english and i need to put it back in the big xml file.
For example when im running the game now i need to see in the main menu Load/Save or Exit or Continue...
But in fact i see in the game long sentesences from the game it self. Like the text is not in the right place.
I tried it few days ago manual just changed manualy in the big xml file i changed the word Laden/Speichern to Load/Save then i saw it was good in the main menu. But now when i changed all the text its all a mess not in the right place.
** Please confirm when you got the files so i will know they sent ok ** Thanks.
Thanks.
danieli
-
Sunday, April 29, 2012 4:48 PM
Danieli,
I recieved the files.
I will check and let you no.
Ferdin Raphael
-
Sunday, April 29, 2012 4:49 PMThank you.
danieli
-
Monday, April 30, 2012 7:33 AM
Hi danieli,
I tried the code with the data you had sent me. I realised I had not taken into account the tags with empty data.
I have modified the addingTranslatedDeponiaFiles() method and have got the expected result on my computer.
Below is the code for the function:
private void addingTranslatedDeponiaFiles() { //int currentLength; //int currentIndex; //int lastIndex = 0; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; //int endTagWidth = endTag.Length; int index = 0; h = new StreamReader(@"D:\test.txt"); line = h.ReadLine(); while (line != null) { if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + line + f.Substring(stringIndex + stringLength); line = h.ReadLine(); } } h.Close(); StreamWriter w = new StreamWriter(@"D:\\New folder (24)\000004ac.xml"); w.AutoFlush = true; w.Write(f); w.Close(); }Hopefully this helps with your code.
Try and let me know if it works
Ferdin Raphael
-
Monday, April 30, 2012 10:50 AM
Ferdin im trying now this fixed function cod.
Could you just tell me what did you change from the last time ? I didnt find any changes ? Could be the line:
line = h.ReadLine(); is the only change ?
Thanks.
danieli
-
Monday, April 30, 2012 11:00 AM
Hi danieli,
Yes. That is the only change.
I repositioned that line so that it would read the next line only when the string is replaced.
This was the problem earlier. When it met an empty tag, it used to still read the next line.
Hence the data was placed at the wrong places in the xml file.
This has been fixed now.
Hopefully, the problem is solved.
Ferdin Raphael
-
Monday, April 30, 2012 12:40 PM
No it dosent work.
My code of the function is like this:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //int currentLength; //int currentIndex; //int lastIndex = 0; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; //int endTagWidth = endTag.Length; int index = 0; using (var fs = new FileStream(@"d:\DeponiaSplit\testingdeponias_Translated.txt", FileMode.Open, FileAccess.Read)) { using (var h = new StreamReader(fs)) { string line; while ((line = h.ReadLine()) != null) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { int percent = (int)(fs.Position * 100 / fs.Length); backgroundWorker1.ReportProgress(percent); if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + line + f.Substring(stringIndex + stringLength); line = h.ReadLine(); if (listBox1.InvokeRequired) { textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = line; })); } } } } h.Close(); } } StreamWriter w = new StreamWriter(@"D:\New folder (24)\000004aa.xml"); w.AutoFlush = true; w.Write(f); w.Close(); }Im not sure if the problem is in the function it self or with the program where i use to reimport back the big xml file back to the data.vis file.
But i remember that if i changed manualy in one place or some places in the xml file the word Laden to Load so when running the game in the main menu i saw Load.
What else can it be ?
In the main menu now i see long sentesences from the game play it self. Is there any possible that something else is wrong with the function ?
danieli
-
Monday, April 30, 2012 1:16 PM
Hi danieli,
The problem was with the condition for the while loop.
I am reading the line before the while loop and then checking if it is null as the condition for the while loop.
The following code, should solve the problem.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //int currentLength; //int currentIndex; //int lastIndex = 0; string startTag = "T256=\""; string endTag = "\""; int startTagWidth = startTag.Length; //int endTagWidth = endTag.Length; int index = 0; using (var fs = new FileStream(@"d:\DeponiaSplit\testingdeponias_Translated.txt", FileMode.Open, FileAccess.Read)) { using (var h = new StreamReader(fs)) { string line; line = h.ReadLine(); while (line != null) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { int percent = (int)(fs.Position * 100 / fs.Length); backgroundWorker1.ReportProgress(percent); if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength != 0) { string test = f.Substring(stringIndex, stringLength); f = f.Substring(0, stringIndex) + line + f.Substring(stringIndex + stringLength); line = h.ReadLine(); if (listBox1.InvokeRequired) { textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = line; })); } } } } h.Close(); } } StreamWriter w = new StreamWriter(@"D:\New folder (24)\000004aa.xml"); w.AutoFlush = true; w.Write(f); w.Close(); }Your menu should show the correct translation now.
Try and tell me if it works.
Ferdin Raphael
-
Monday, April 30, 2012 3:12 PM
Ferding
No still not working good.
I also saw in some of the big xml in some places that its still in German.
And yet in the main menu i see long sentecenses .
I will try again but for now it didnt work.
danieli
-
Monday, April 30, 2012 3:25 PM
Ferdin
I just edited the two big xml files one the original in german and the second one in english.
I did Edit>Find for : T255 T12="Unnamed" T13="144337" T14="0" T55="201" T257="" T256=
And in the german xml file the original one in this location there is the words Laden/Speichern ( Load/Save in english ) :
T255 T12="Unnamed" T13="144337" T14="0" T55="201" T257="" T256="Laden/Speichern"
But in the english big xml file in the same location i see :
T255 T12="Unnamed" T13="144337" T14="0" T55="201" T257="" T256="Ksch!"
So for sure something is wrong. The question is where. At the function or maybe i did something wrong with the files. I will try again but it seems that something is wrong.
danieli
-
Monday, April 30, 2012 5:51 PM
There is something wrong with the extracting taking out the german text from the big xml.
And then when it put back the text it dosent in the right place.
Thats my guess.
What i tried now is to cut the big translated text file to 29 small files each one i translated manual in google translate.
Then i used my code to put the small files back to one big text file. Then i used the code to add the lines from the text file back to the xml file.
Now when i try to run the game its giving me text file error report:
:778516: parser error : attributes construct error
T12="Unbenannt" T13="2257" T14="0" T55="-1" T257="" T256="Look after your ... "
^
:778516: parser error : Couldn't find end of Start Tag T255 line 778516
T12="Unbenannt" T13="2257" T14="0" T55="-1" T257="" T256="Look after your ... "
^
:778518: parser error : Opening and ending tag mismatch: T254 line 777561 and T255
</T255>
^
:778930: parser error : attributes construct error
<T255 T12="Unbenannt" T13="3154" T14="0" T55="-1" T257="" T256=""I also like t
^
:778930: parser error : Couldn't find end of Start Tag T255 line 778930
<T255 T12="Unbenannt" T13="3154" T14="0" T55="-1" T257="" T256=""I also like t
^
:778932: parser error : Opening and ending tag mismatch: T100 line 2 and T255
</T255>
^
:778933: parser error : Extra content at the end of the document
<T255 T12="Unbenannt" T13="3161" T14="0" T55="163" T257="" T256="But deep insi
^
20:39:51: Error: Error loading game from file 'data.vis'!
20:39:51: Error: Init failed, could not load gameThe data.vis 43mb file wich also contain inside the xml file give this errors.
So its for sure something is wrong either with extracting the text from the big xml first time or something wrong with adding the text back to the xml.
This is my code to extract retrive the text from the xml ( im not sure if the code is good enough and im not sure if its retriving all the text in the xml but for now im not sure if its good at all the code ):
private void test() { int countFiles = 0; byte[] a; string startTag = "T256=\""; string endTag = "\""; int index = 0; int startTagWidth = startTag.Length; int endTagWidth = endTag.Length; int fileLength = f.Length; w = new StreamWriter(@"d:\testingdeponias.txt"); while (true) { if (index > f.LastIndexOf(startTag)) { break; } int startTagIndex = f.IndexOf(startTag, index); int stringIndex = startTagIndex + startTagWidth; index = stringIndex; int endTagIndex = f.IndexOf(endTag, index); int stringLength = endTagIndex - stringIndex; if (stringLength == 0) { } else { string test = f.Substring(stringIndex, stringLength); if (test.Contains("<pa>")) { string t = "<pa>"; int y = t.Length; string test1 = test.Substring(0, stringLength - y); listBox1.Items.Add(test1); w.WriteLine(test1); } else { listBox1.Items.Add(test); w.WriteLine(test); } } } w.Close(); }Then i split and combine back the text file after retriving the text from the big xml i split it to many small files translate them then combine them back:
private bool SplitFiles(string largeFile, int preferredSize, string savePath) { bool bRet = false; if (File.Exists(largeFile)) { StreamReader sr = null; StreamWriter sw = null; try { Directory.CreateDirectory(savePath); sr = new StreamReader(largeFile); char[] allchars = sr.ReadToEnd().ToCharArray(); sr.Close(); int i = 0; int position = 0; while (position < allchars.Length) { int l = Math.Min(preferredSize, allchars.Length - position); sw = new StreamWriter(Path.Combine(savePath, "File" + i.ToString("D4") + ".txt")); sw.AutoFlush = true; char[] buffer = new Char[l]; Array.Copy(allchars, position, buffer, 0, l); sw.Write(buffer); position += l; i++; sw.Close(); bRet = true; } } catch { bRet = false; } finally { sr.Close(); sr = null; sw.Close(); sw = null; } } return bRet; } private bool CombineFiles(string newLargeFile, string savePath) { bool bRet = false; if (Directory.Exists(savePath)) { StreamReader sr = null; StreamWriter sw = null; try { List<FileInfo> fList = new List<FileInfo>(); //fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").ToArray()); //fList = fList.OrderBy(a => a.Name).ToList(); fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").Where(a => a.FullName.ToLower().Equals(newLargeFile.ToLower()) == false).ToArray()); fList = fList.OrderBy(a => a.Name).ToList(); sw = new StreamWriter(newLargeFile); sw.AutoFlush = true; foreach (FileInfo fi in fList) { sr = new StreamReader(fi.FullName); sw.Write(sr.ReadToEnd()); sr.Close(); } } catch { bRet = false; } finally { sr.Close(); sr = null; sw.Close(); sw = null; } } return bRet; }Something is wrong in this 4 functions. The retriving or the split or the combine or the adding back the text to the xml.
I could also edit my own the xml file now fix this errors the game ius giving when trying to run it but it wont work the text will not be show good in the game.
Something is wrong with this functions or in one of them.
danieli
-
Friday, May 04, 2012 2:10 AMModerator
Hi danieli,
Since Ferdin has offered lots of support on this case, could you please let us know the status now? Also, it's a very long thread, could you please provide some summary of the issue here? If you can provide some test files as well, that would be quite helpful for us to give some suggestions.
Have a nice weekend!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
Friday, May 04, 2012 7:47 AM
Hi danieli,
Since Ferdin has offered lots of support on this case, could you please let us know the status now? Also, it's a very long thread, could you please provide some summary of the issue here? If you can provide some test files as well, that would be quite helpful for us to give some suggestions.
Have a nice weekend!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
Ferdin indeed offered a lot of help. The problem is that the text file im reading line by line and put back each line to big xml file as it was when i retrived/extracted the text from the xml is not in the right place. I mean when im running the game for example in the main menu instead see Load Save...I see long sentecenses from the game it self.
Now this is a link to the big xml file: https://skydrive.live.com/redir.aspx?cid=3b8a7d9f66ff985b&resid=3B8A7D9F66FF985B!129&parid=3B8A7D9F66FF985B!126&authkey=!AAUKtC-exwxOSKQ
And the Functions im using wich are Ferdin offered are above.
The main idea is to read the big xml file in the first time and take out extract all the text into a new text file. Then to translate the text file to english and then to read line by line the text file and put the text back to the exact places it was in the big xml file.
So in general Ferdin offers help and almost do the right thing but the problem is that the text when its back to the xml isnt in the right place.
Im not sure why its like that.
danieli
-
Tuesday, May 08, 2012 10:02 AMModerator
Hi danieli,
I will try to involve some senior engineers.
Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us

