Little problem with a function for a total newbie ...
hi guys ... I'm a C# newbie, just starting to learn and i am having a problem I can't seem to fix.
i have a form with browse button, to select a directory in which i will look for some files. for example, i have a file named "map.i3d" and when found, declared as mapI3Dfile in my project.
i have a function that is supposed to add 2 strings at precise places in the file in case of one or two don't exist. And if insertion of string1 is done correctly, a message is sent to a richTextBox. if no need to insert the string, another message is sent to the textbox. same thing for string2.
the function does exactly what I want for string1, so I used the same "routine" to check and then eventually to insert string2. the insertion is perfectly done but the problem is pretty simple.
No matter string2 is not already present in the file, the script thinks the string2 thinks the string2 is present, so it returns the bad insertion state. Here is below the code of the function that is handling that part... If someone can help me, that would be greatly appreciated !!
Code of the function ===> https://pastebin.com/cnWUE051
Pastebin
` private void HandleI3DInsertion(string mapI3Dfile) ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
59 Replies
it is hard to say anything without knowing what the file and what the tag are
would you need the whole code of Form1.cs ??
here is the complete actual code for Form1.cs : https://pastebin.com/QDtQxHVN
Pastebin
using System;using System.Collections.Generic;using System.Drawing;...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
no, not the form, the file you are reading
and what tag2 is
ok ...
the file is 190000+ lines xml file but I'll paste the part where the 2 tags are supposed to be inserted ...
tag1="<!-- NEW FOLIAGES -->"
tag2="<!-- END OF NEW FOLIAGES -->"
Part of the code of the file :
<?xml version="1.0" encoding="iso-8859-1"?>
<i3D name="map.i3d" version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://i3d.giants.ch/schema/i3d-1.6.xsd">
<Asset>
<Export program="GIANTS Editor 64bit" version="9.0.4"/>
</Asset>
<Files>
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
<File fileId="1831" filename="$data/fillPlanes/barley_specular.dds"/>
<File fileId="1832" filename="$data/fillPlanes/canola_diffuse.dds"/>
<File fileId="1833" filename="$data/fillPlanes/canola_normal.dds"/>
<File fileId="1834" filename="$data/fillPlanes/canola_specular.dds"/>
<File fileId="1687" filename="$data/fillPlanes/cotton_diffuse.png"/>
<File fileId="1688" filename="$data/fillPlanes/cotton_normal.png"/>
<File fileId="1689" filename="$data/fillPlanes/cotton_specular.png"/>
I want to insert tag1 on the first line after the one containing "<Files>" and tag2 before the first one containing "<File fileId=" ... and then return a value to a variable so that the rest of the script will know what to write in the text box ... you see ?
you should really be using an XML reader for this, not string manipulations... it is much more difficult to get it right when you are doing it this way
but, uh, i still don't understand your question
what u mean ? what do i need to clarify ?
i still don't really understand what doesn't work about your code
i'm a total beginner, i have to admit i used ChatGPT to build some sample code to understand the basics, but i can't find a way to get this crappy display to my textbox fixed ...
with this Form1.cs everything works perfectly, except when tag2 is inserted, it returns the wrong insertion status code, so that the display to the textbox is not good ... you see ?
it would help to know what you are expecting to see and what you are actually seeing
the base file is the part i pasted above, and the same part with the tags inserted is :
<?xml version="1.0" encoding="iso-8859-1"?>
<i3D name="map.i3d" version="1.6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://i3d.giants.ch/schema/i3d-1.6.xsd">
<Asset>
<Export program="GIANTS Editor 64bit" version="9.0.4"/>
</Asset>
<Files>
<!-- NEW FOLIAGES -->
<!-- END OF NEW FOLIAGES -->
<File fileId="1829" filename="$data/fillPlanes/barley_diffuse.dds"/>
<File fileId="1830" filename="$data/fillPlanes/barley_normal.dds"/>
<File fileId="1831" filename="$data/fillPlanes/barley_specular.dds"/>
then in the textbox, if tag1 is inserted i want to see : "Insert I3D Tag 1 = OK", if tag2 is inserted I want to see "Insert I3D Tag 2 = OK"...
if tag1 is already present in the file, then it should disply "Insert Tag 1 = N/A", and if tag2 is present, it should display "Insert I3D Tag 2 = N/A"
ok. sure. what makes you think it is not working, though
only the display in the textbox (kind of a log box) is wrong, the insertion works perfectly ...
even if it is not present, and then inserted, status for tag2 is set to display N/A while i want that it displays OK
i applied the same logic for insertion and status of tag1 and it works perfectly, can't figure out why it does not work the same way for tag2
well. it does not do that for me
i have looked at your code, pasted it into my own project, made a test file, and I get
after passing it a file which contains this XML in it
there is a problem, and it's that you code does not write the file correctly
once i look at the file after it's written, i get this:
there should be a
<!-- NEW FOLIAGES -->
, presumablyi pasted the wrong code for the Form1.cs ... i'm so sorry 😢
here is the good version : https://pastebin.com/Bcccs7hR
it is producing an error upon inserting tag2, but can't understand it
Pastebin
using System;using System.Collections.Generic;using System.Drawing;...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
but even if it is producing an error message window, both tags are inserted correctly
now this code needs to be fixed about that error message, and about the not correct status written in the textbox ... i'm pretty sure that it is a small small thing but as i'm a terrible newbie, i am totally unable to fix that thing
this code has the same behavior for me
when i try it on this file: it does not print an error, and it prints OK for both of them
it still does not write the comments correctly
i still get without the starting comment
then, only after i run it again, does it do what you say it does
even with the latest Form1.cs code i pastbined ?
yes
the problem is pretty clear
huuuh
the error i'm getting
yes, i also get this error
and the app window with the textbox ...
only after i have already run it once, after it has messed up the file
huuuh so weird ... 😦
this makes complete sense
your file is already broken
you are re-running it on the broken file
yes. this is expected. the fix is to not break the file
no my file, map.i3d, is not broken, i always reuse the orignal non modified file
your code modifies the file that you read from
unless you are copying it over every time you try, it is the same one
any way that you can help me fixing all that mess ???
your
lines
here does not contain tag1
you are reading the file into lines
then writing the new version of the file with tag1
then overwriting it, with only tag2
i have to admit that i did not write/produced this function ... i tried to but with my limited knowledge, i was not able to get any result
i'm on that problem for the last 2 days, like 10-12hrs a day ...i think i have to stop here, forget a good reward from my school class, and use my tiny bash scripts that are way less nice looking
i would suggest changing this method to look like this:
this fixes the issue for me
which function ? HandleI3DInsertion ? Other ?
InsertTagBeforeLine
the one that it looks similar to
that part should replace what i have in the "try { ... }" part , right ?
yes
you're right it has fixed the error window thing ... one good thing thanks !
but still reporting the wrong status code (i see N/A for both tags, while it should have been OK)
that's because your file already contains them
looks like the script scans the file AFTER it has added the tags
i am sorry but i do not believe you when you say you are using the blank files
the code does not lie
i can swear on my mother's soul (gone 3 months ago) that I copy the original file over the modified/broken one everytime i try the app
but could it happen like this if the scan is launched AFTER the insertions are done ? that would explain why it returns a "already present" state ...
ok. well, we can show this and it would not be too hard:
what does the message box say
oh. i see what you mean
sorry , i was thinking of the first code you sent
i tried the messagebox thing, the app is using like 100% of CPU and shows nothing ... normal ?
yes i see what is happening, it is true that it is doing the scan after the insertions are done
in your first code it was not like that. well, kinda
yes, it was not the good one, i'm sorry for posting the wrong one at first
anyway, the issue is in
goButton_Click
here, indeed, you are inserting the tags before you do the scanso i have to "swap" those 2 lines ?
yes
ok let's try it
it worked partly, the good reports status, but added the tags twice
oh. sorry. i missed part of your code
and if i click on Go a second time, it added the tags a third time, reporting it did not add anything
i did not see that you were still doing
InsertTagBeforeLine
inside of HandleI3DInsertion
you need to delete InsertTagBeforeLine
completely from goButton_Click
ok let's try it
it now works like a charm ... thank you so much !!! you saved my school exam ... you can't imagine how important it is for me to get this exam so that I would honor the memory of my mother !!
:-) i am glad that it is working for you
😉