By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Regarding changing the value of a node in xml file

P: 2
hi all,

let me explain u my problem in detail.i m trying to develop an application in c#.net. i have to show a form in wich i hav to take a check box & a quit menu.wen i press the quit menu,the application shud check the state of check box & store that value inside a tag in a xml file & then the application shud exit.now wen i again run the application the program shud read the value of checkbox & show that in form i.e whether it is checked or not.that xml file is already created manually with some tags.i have to modify the value within the tag <checkboxstate>. plz help me out to do this..

here by m attaching my source code & xml file

//source code
Expand|Select|Wrap|Line Numbers
  1. namespace Checkboxapplication
  2. {
  3.     public partial class selectionform : Form
  4.     {
  5.         private XmlDocument doc;
  6.         string data;
  7.         private string path = "/Program Files/Checkboxapplication/deepika.xml";
  8.  
  9.         public selectionform()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void quitmenu_Click(object sender, EventArgs e)
  15.         {
  16.             if (checkBox.Checked == true)
  17.                 data = "one";
  18.             else if (checkBox.Checked == false)
  19.                 data = "zero";
  20.             WritetoXmlfile();
  21.             Application.Exit();
  22.         }
  23.  
  24.         private void checkBox_CheckStateChanged(object sender, EventArgs e)
  25.         {
  26.  
  27.         }
  28.  
  29.         private void selectionform_Load(object sender, EventArgs e)
  30.         {
  31.  
  32.             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  33.             doc = new XmlDocument();
  34.             doc.Load(fs);
  35.             XmlNodeList nodes = doc.GetElementsByTagName("Root");
  36.             for (int i = 0; i < nodes.Count; i++)
  37.             {
  38.                 XmlNodeList list = nodes[i].ChildNodes;
  39.                 for (int j = 0; j < list.Count; j++)
  40.                 {
  41.                     if (list[j].Name == "checkboxstate")
  42.  
  43.                         data = list[j].InnerText;
  44.                 }
  45.             }
  46.             data = data.Trim();
  47.             if (data == "one")
  48.                 checkBox.Checked = true;
  49.             else if (data == "zero")
  50.                 checkBox.Checked = false;
  51.             fs.Close();
  52.  
  53.         }
  54.  
  55.       void WritetoXmlfile()
  56.         {
  57.             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  58.             doc = new XmlDocument();
  59.             doc.Load(fs);
  60.             XmlNodeList nodes = doc.GetElementsByTagName("Root");
  61.             for (int i = 0; i < nodes.Count; i++)
  62.             {
  63.                 XmlNodeList list = nodes[i].ChildNodes;
  64.                 for (int j = 0; j < list.Count; j++)
  65.                 {
  66.                     if (list[j].Name == "checkboxstate")
  67.  
  68.                         list[j].InnerText = data;
  69.                 }
  70.             }
  71.             doc.Save(fs);
  72.             fs.Close();
  73.  
  74.          }
  75.  
  76.       public static void main()
  77.         {
  78.             selectionform frm = new selectionform();
  79.             Application.Run(frm);
  80.         }
  81.     }
  82. }
//xml file (deepika.xml)

<Root>
<checkboxstate>zero</checkboxstate>
</Root>
Nov 6 '07 #1
Share this Question
Share on Google+
2 Replies


P: 18
Try below the code.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.XPath;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

public partial class Testxml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XmlDocument xmlDoc = new XmlDocument();
FileStream fs = new FileStream(Server.MapPath("Serializexml.xml"), FileMode.Open, FileAccess.ReadWrite);
xmlDoc.Load(fs);
string checkBoxState = xmlDoc.SelectSingleNode("Root/checkboxstate").InnerText;
if (Convert.ToBoolean(checkBoxState) == true)
{
chkSelection.Checked = true;
}
fs.Close();
}
}
protected void btnMenu_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
FileStream fs = new FileStream(Server.MapPath("Serializexml.xml"), FileMode.Open, FileAccess.ReadWrite);
xmlDoc.Load(fs);
fs.Close();
XmlNode newNode=xmlDoc.SelectSingleNode("Root/checkboxstate");
if (chkSelection.Checked == true)
{
newNode.InnerText = "true";
}
else
{
newNode.InnerText = "false";
}
FileStream fs1 = new FileStream(Server.MapPath("Serializexml.xml"), FileMode.Truncate, FileAccess.ReadWrite);
xmlDoc.Save(fs1);
fs1.Close();
}
}
Nov 6 '07 #2

P: 18
Change the last 3 lines to

xmlDoc.Save(Server.MapPath("Serializexml.xml"));

Not necessary to create a new filestream..
Nov 7 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.