XML editor using C#

I want to edit xml file using C#, I am able to read it but facing prob

Page 1 of 1

0 Replies - 1095 Views - Last Post: 17 July 2009 - 10:40 AM Rate Topic: ***-- 2 Votes

#1 prashant_kerkar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 17-July 09

XML editor using C#

Posted 17 July 2009 - 10:40 AM

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

namespace TestRad
{
	public partial class Form8 : Form
	{
		private XmlDocument xmlDoc;

		public Form8()
		{
			InitializeComponent();
		}

		private void Form8_Load(object sender, EventArgs e)
		{
			xmlDoc = new XmlDocument();
			LoadDocument();

			
		}


		private void LoadDocument()
		{
			try
			{
				// Load the XML data into it.	 
				xmlDoc.Load("hosts.xml");

				// Initialize the TreeView control.
				tvTree.Nodes.Clear();
				tvTree.Nodes.Add(new TreeNode(xmlDoc.DocumentElement.Name));
				TreeNode tNode = new TreeNode();
				tNode = tvTree.Nodes[0];

				// Populate the TreeView with the DOM nodes.
				AddNode(xmlDoc.DocumentElement, tNode);
				tvTree.ExpandAll();
			}
			catch (XmlException xmlEx)
			{
				MessageBox.Show(xmlEx.Message);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void AddNode(XmlNode xmlNode, TreeNode treeNode)
		{
			XmlNode xNode;
			TreeNode tNode;
			XmlNodeList xNodeList;
			int i;

			// Loop through the XML nodes until the leaf is reached.
			// Add the nodes to the TreeView during the looping process.
			if (xmlNode.HasChildNodes)
			{
				xNodeList = xmlNode.ChildNodes;
				for (i = 0; i <= xNodeList.Count - 1; i++)
				{
					xNode = xmlNode.ChildNodes[i];
					TreeNode newNode = new TreeNode(xNode.Name);

					
						treeNode.Nodes.Add(newNode);
						tNode = treeNode.Nodes[i];

						AddNode(xNode, tNode);
					
					
				}
			}
			else
			{
				// Here you need to pull the data from the XmlNode based on the
				// type of node, whether attribute values are required, and so forth.
				treeNode.Text = (xmlNode.OuterXml).Trim();
			}
		}

		private void tvTree_AfterSelect(object sender, TreeViewEventArgs e)
		{
			//=e.Node.n
			if (e.Node.Nodes.Count == 0)
			  textBox1.Text = e.Node.Text;
			else
				textBox1.Text = "";
			
				
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			tvTree.SelectedNode.Text = textBox1.Text;
			tvTree.Refresh();

			//TODO: Traverse the tree recursively and write the nodes to XML file; and reload the document
			
			//LoadDocument(); //remove comment from this to reload the document....
		}
	}
}
  


Is This A Good Question/Topic? 0
  • +

Page 1 of 1