Hey, I'm trying to make a program where you have the file, which is a config.xml.

I'm basically going to have the user type in numbers into a textbox, and when they hit a button, it will search for the first few characters in the file, delete the whole line, and replace it with text that I have plus the user input.

I have found a bunch of codes on replacing specified text and strings in a file, but I can't find anything that can find just the first few letters / characters, delete the whole line, and then help me carry on.

If you have any helpful codes, I'd be very appreciative thanks.

If that file is XML document then use XML parsers.

If that file is XML document then use XML parsers.

Well I used some codes regarding txt files only but they worked just fine for this xml. And for the parsers, I still wouldn't know what to do lol. I'd have to see some examples first

bump. sorry I was trying to read some articles on xml parsers but I'm still having a hard time. Any help?

What did you expect it to do? Do you get any errors? Please post program code and xml file content you are working on.

Sorry was workign on something, but this is the content of my xml.

I've been trying a lot of different things, I'm a bit closer, this is what I got so far, button 2 is what I'm currently working on at the moment.

Also, as you see, the <IP>0.0.0.0</IP> I'm trying to replace the innards with userinput ip from a textbox. I would get this a bit easier if the ip was always the same, but in different server clients, the <IP>" "</IP> is always different so I'm trying to replace the entire line with a new one.

<?xml version="1.0"?>
<XML>
	<SERVER>
		<IP>0.0.0.0</IP>
		<PORT>6000</PORT>
	</SERVER>

	<SKIN>Default</SKIN>

	<VIDEO>
		<WIDTH>1024</WIDTH>
		<HEIGHT>768</HEIGHT>
		<COLORBITS>32</COLORBITS>
		<GAMMA>255</GAMMA>
		<FULLSCREEN>TRUE</FULLSCREEN>
		<REFLECTION>true</REFLECTION>
		<LIGHTMAP>true</LIGHTMAP>
		<DYNAMICLIGHT>true</DYNAMICLIGHT>
		<SHADER>true</SHADER>
		<CHARACTORTEXTURELEVEL>0</CHARACTORTEXTURELEVEL>
		<MAPTEXTURELEVEL>0</MAPTEXTURELEVEL>
		<EFFECTLEVEL>0</EFFECTLEVEL>
		<TEXTUREFORMAT>1</TEXTUREFORMAT>
		<NHARDWARETNL>false</NHARDWARETNL>
	</VIDEO>

	<AUDIO>
		<BGM_MUTE>true</BGM_MUTE>
		<BGM_VOLUME>0.607800</BGM_VOLUME>
		<EFFECT_MUTE>false</EFFECT_MUTE>
		<EFFECT_VOLUME>0.986200</EFFECT_VOLUME>
		<SOUND_3D>1</SOUND_3D>
		<A8BITSOUND>false</A8BITSOUND>
		<INVERSESOUND>false</INVERSESOUND>
		<HWMIXING>false</HWMIXING>
		<HITSOUND>false</HITSOUND>
		<NARRATIONSOUND>false</NARRATIONSOUND>
	</AUDIO>

	<MOUSE>
		<SENSITIVITY>0.399800</SENSITIVITY>
		<INVERT>FALSE</INVERT>
	</MOUSE>

	<JOYSTICK>
		<SENSITIVITY>1.000000</SENSITIVITY>
		<INVERT>FALSE</INVERT>
	</JOYSTICK>

	<KEYBOARD>
		<FORWARD alt="-1">17</FORWARD>
		<BACK alt="-1">31</BACK>
		<LEFT alt="-1">30</LEFT>
		<RIGHT alt="-1">32</RIGHT>
		<MELEEWEAPON alt="-1">18</MELEEWEAPON>
		<PRIMARYWEAPON alt="-1">16</PRIMARYWEAPON>
		<SECONDARYWEAPON alt="-1">33</SECONDARYWEAPON>
		<ITEM1 alt="-1">5</ITEM1>
		<ITEM2 alt="-1">6</ITEM2>
		<PREVOUSWEAPON alt="-1">256</PREVOUSWEAPON>
		<NEXTWEAPON alt="-1">257</NEXTWEAPON>
		<RELOAD alt="-1">19</RELOAD>
		<JUMP alt="-1">57</JUMP>
		<SCORE alt="-1">15</SCORE>
		<MENU alt="-1">-1</MENU>
		<TAUNT alt="-1">41</TAUNT>
		<BOW alt="-1">7</BOW>
		<WAVE alt="-1">8</WAVE>
		<LAUGH alt="-1">9</LAUGH>
		<CRY alt="-1">10</CRY>
		<DANCE alt="-1">11</DANCE>
		<SCREENSHOT alt="-1">88</SCREENSHOT>
		<RECORD alt="-1">87</RECORD>
		<DEFENCE alt="-1">42</DEFENCE>
		<TOGGLECHAT alt="-1">47</TOGGLECHAT>
		<USEWEAPON alt="-1">258</USEWEAPON>
		<USEWEAPON2 alt="-1">259</USEWEAPON2>
	</KEYBOARD>

	<MACRO>
		<F1>^1Enemy spotted!</F1>
		<F2>^2Medding/^3Reloading</F2>
		<F3>^46Require ^5assistance, ^2come quickly</F3>
		<F4>^1S, ^9B, ^1IG, or ^9IS</F4>
		<F5>^4Hello!</F5>
		<F6>Let's have a good time and play fair.</F6>
		<F7>/laugh</F7>
		<F8>/taunt</F8>
		
	</MACRO>

	<ETC>
		<NETWORKPORT1>7700</NETWORKPORT1>
		<NETWORKPORT2>7800</NETWORKPORT2>
		<BOOST>FALSE</BOOST>
		<REJECT_NORMALCHAT>FALSE</REJECT_NORMALCHAT>
		<REJECT_TEAMCHAT>FALSE</REJECT_TEAMCHAT>
		<REJECT_CLANCHAT>FALSE</REJECT_CLANCHAT>
		<REJECT_WHISPER>FALSE</REJECT_WHISPER>
		<REJECT_INVITE>FALSE</REJECT_INVITE>
		<CROSSHAIR>2</CROSSHAIR>
	</ETC>

	
</XML>
private void button2_Click(object sender, EventArgs e)
        {
            string intext = "<IP>"; //<IP>0.0.0.0</IP>
            string/*[]*/newtxt = "<IP>" + textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + "." + textBox4.Text + "</IP>";
            string config = Desktop + @"\config.xml";



            StreamReader sr = new StreamReader(config);
            string content = sr.ReadToEnd();
            //content = intext.Replace(Environment.NewLine, "");
            content = Regex.Replace(content, intext, newtxt);
            sr.Close();
            /*
            using (StreamWriter sw = new StreamWriter(config))
            {
                foreach (string line in newtxt)
                {
                    if (line.Contains("<IP>") == true)
                    {
                        sw.WriteLine(line);
                    }
                }
            }
            */
            StreamWriter sw = new StreamWriter(config);
            sw.Write(content, Environment.NewLine);
            sw.Close();


            List<string> linelist = File.ReadAllLines(config).ToList();
            linelist = linelist.Where(x => x.IndexOf("<IP>") <= 0).ToList();
            File.WriteAllLines(config, linelist.ToArray());
            
        }
    }
}

Why not XML-Linq or XML Dom?

XML-LINQ - to get/set node value

using System.Xml;
using System.Xml.Linq;
....
...
void test() {
           string config =   @"config.xml";
           XDocument doc = XDocument.Load(config);
           foreach (var t in doc.Descendants("IP"))
           {
               t.Value = "10.10.10.10";
           }
           doc.Save(config);
}

Never really knew about those. I'm still a beginner, and thanks for your help, I will try right now !

I used your code, and well, I used my original string newtxt, because it's supposed to be from userinput, and when I loaded it, I got this

<IP>&lt;IP&gt;6.20.542.8&lt;/IP&gt;</IP>

That ip was just random one I put into the boxes

...
using System.Xml;
using System.Xml.Linq;
....
...
void test() {
           string config =   @"config.xml";
           XDocument doc = XDocument.Load(config);
           foreach (var t in doc.Descendants("IP"))
           {
              string newtxt =textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + "." + textBox4.Text;

               t.Value = newtxt;
           }
           doc.Save(config);
}

Yes, that's what I did and I got that =[

oh nvm, I see, no <IP>'s in the string, let me try again ahah thx

edit : Awesome, it worked nicely. But one thing, in the xml, all the spaces are taken out, I mean like the spaces between the lines, the line breaks are gone xD

public void RenameFileOrDirectory(string sourceFileName, string destFileName)
{
    try
    {
        if (!sourceFileName.Equals(destFileName))
        {
            FileAttributes attr = File.GetAttributes(sourceFileName);

            if ((attr & FileAttributes.Directory).Equals(FileAttributes.Directory))
            {
                Directory.Move(sourceFileName, destFileName);
            }
            else if ((attr & FileAttributes.ReadOnly).Equals(FileAttributes.ReadOnly))
            {
                Console.WriteLine("..that is read-only");
            }
            else
            {
                File.Copy(sourceFileName, destFileName);
                File.Delete(sourceFileName);
            }
        }
        else
        {
            Console.WriteLine("..epic failure");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

public void Replace(string file, string search, string repl)
{
    if (File.Exists(file))
    {
        string[] lines = File.ReadAllLines(file);

        StreamWriter sw = new StreamWriter(new FileStream(Environment.CurrentDirectory + "\\temp.txt~", FileMode.Create, FileAccess.Write));

        foreach (string line in lines)
        {
            string newLine = line.Replace(search, repl);
            sw.WriteLine(newLine);
        }

        sw.Close();

        try
        {
            File.Delete(file);
            this.RenameFileOrDirectory(Environment.CurrentDirectory + "\\temp.txt~", file);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.