simple streamwriter question


This is what i have so far:

private void radioButton9_CheckedChanged(object sender, EventArgs e)
{

devicesConfigurationFile_ = @".\Devices.xml";

using (StreamWriter sw = new StreamWriter(@".\Devices.xml"))
{



messageNode_ = "//DEVICES/DEVICE_LIST/MESSAGE_LIST_RECEIVE/MESSAGE/Login";
XMLDocNodes_ = XMLDoc_.SelectNodes(messageNode_ + "/Login");
sw.Write("no");
}

Its writing a new text file every time the button is clicked when i just want it to append an existing file after parsing through to the correct attribute. The file it will be parsing through is XML. Any help would be great as im a huge noob into C#.


Thanks





Answer this question

simple streamwriter question

  • Stas Kravets

    Try this.. Pass "true" so it appends if a file already exists...

    using (StreamWriter sw = new System.IO.StreamWriter(@"c:\Devices.xml", true))

    {

    messageNode_ = "//DEVICES/DEVICE_LIST/MESSAGE_LIST_RECEIVE/MESSAGE/Login";

    XMLDocNodes_ = XMLDoc_.SelectNodes(messageNode_ + "/Login");

    sw.Write("no");

    }


  • LouisVanAlphen

    thanks! But now it writes the word "no" at the very end of the XML file. I guess im not parsing correctly. Is my chunk of code the correct method for parsing down to an XML file attribute and replacing it

    Thanks



  • Aleniko29139

    basically im trying to parse down an XML file to the "login" node, and change the attribute value from yes to no, and visa versa. The code snip is what happens when a radio button is clicked.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.IO;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {
    private string devicesConfigurationFile_ = String.Empty;
    private XmlDocument XMLDoc_ = new XmlDocument();
    private XmlNode XMLDocNode_;
    private XmlNodeList XMLDocNodes_;
    private StreamWriter sw = new StreamWriter(new FileStream("Log.txt", FileMode.OpenOrCreate));

    private string messageNode_ = "//DEVICES/DEVICE_LIST/MESSAGE_LIST_RECEIVE/MESSAGE/Login";

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    sw.WriteLine("We are Statting");

    devicesConfigurationFile_ = @".\Devices.hlx";
    LoadXMLDoc();

    //Populate the Configured Device CheckList from device
    // hlx file
    XMLDocNode_ = XMLDoc_.SelectSingleNode("//DEVICES/DEVICE_LIST");
    XMLDocNodes_ = XMLDocNode_.ChildNodes;
    //XMLDocNodes = XMLDoc.SelectNodes("//DEVICES/DEVICE_LIST/MESSAGE_LIST_RECEIVE/MESSAGE"); //XMLDocNode.ChildNodes;

    //Populates the comboBox1 with item names
    foreach (XmlNode XMLDocNode2 in XMLDocNodes_)
    {
    if (XMLDocNode2.Name == "DEVICE")
    {
    comboBox1.Items.Add(XMLDocNode2.Attributes["name"].Value.ToString());
    }
    }
    }

    private void LoadXMLDoc()
    {
    try
    {
    XMLDoc_.Load(devicesConfigurationFile_);

    //XMLDoc_.Save(devicesConfigurationFile_);
    }
    catch (XmlException xmlException)
    {
    MessageBox.Show("Devices.hlx is corrupt in some way: " + xmlException.Message.ToString());
    }
    catch (UnauthorizedAccessException accessExcpetion)
    {
    }
    catch
    {
    }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    string temp = comboBox1.SelectedItem.ToString();


    Device deviceInstance = new Device();

    XMLDocNodes_ = XMLDoc_.SelectNodes(messageNode_);

    //Populates the comboBox1 with item names
    foreach (XmlNode XMLDocNode2 in XMLDocNodes_)
    {
    if (XMLDocNode2.Attributes["id"].Value == "1")
    {
    deviceInstance.SendLogin = GetYesNoValueAsBool(XMLDocNode2.Attributes["selected"].Value.ToString());
    }
    if (XMLDocNode2.Attributes["id"].Value == "2")
    {
    deviceInstance.SendLogout = GetYesNoValueAsBool(XMLDocNode2.Attributes["selected"].Value.ToString());
    }
    if (XMLDocNode2.Attributes["id"].Value == "3")
    {
    deviceInstance.SendOpen = GetYesNoValueAsBool(XMLDocNode2.Attributes["selected"].Value.ToString());
    {
    if (XMLDocNode2.Attributes["id"].Value == "4")
    {
    deviceInstance.SendUpdate = GetYesNoValueAsBool(XMLDocNode2.Attributes["selected"].Value.ToString());


    }
    }
    }
    }

    }

    private bool GetYesNoValueAsBool(string yesOrNo)
    {
    if (yesOrNo == "yes")
    {
    return true;
    }

    if (yesOrNo == "no")
    {
    return false;
    }
    MessageBox.Show("Value for yes or no string is " + yesOrNo + "This in not a correct value");
    return false;
    }

    private string GetTrueFalseValueAsStringYesNo(bool trueFalse)
    {
    //if (yesOrNo == "yes")
    //{
    // return true;
    //}

    //if (yesOrNo == "no")
    //{
    // return false;
    //}
    //MessageBox.Show("Value for yes or no string is " + yesOrNo + "This in not a correct value");
    return String.Empty;
    }

    private void button2_Click(object sender, EventArgs e)
    {



    MessageBox.Show("Default vales set");
    }

    /// <summary>
    /// This is the apply button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {



    MessageBox.Show("Settings applied");
    }

    private void button3_Click(object sender, EventArgs e)
    {
    try
    {
    sw.WriteLine("Ending");
    Application.Exit();
    }
    catch (ApplicationException Appexception)
    {
    }
    Application.Exit();

    }











    private void radioButton9_CheckedChanged(object sender, EventArgs e)
    {

    devicesConfigurationFile_ = @".\Devices.hlx";

    using (StreamWriter sw = new StreamWriter(@".\Devices.hlx", true))
    {

    messageNode_ = "//DEVICES/DEVICE_LIST/MESSAGE_LIST_RECEIVE/MESSAGE/Login";

    XMLDocNodes_ = XMLDoc_.SelectNodes(messageNode_ + "/Login");

    sw.Write("no");

    }


  • derekyeong

    Can you post some detailed sample code and give more info about what you are trying to do...
  • bhavu

    sorry forgot some close brackets off the end of that

    }


    }





    }




  • Noa

    As suggested by sbogollu,

    pass second bool parameter to StreamWriter's constructor which specify that should it be appended or re created.

    pass true to append.

    Best Regards,

    Rizwan aka RizwanSharp



  • simple streamwriter question