Tuesday, November 23, 2010

Working with xml file in C#

Well, Currently I am working with a project called webcam desktop application which mainly captures snapshot and video from the webcam. Before, starting the project, I had very little concept about how to do that and before me, an experienced developer worked on it. Anyway, my task was to fix the bugs. Whenever, I started bug-fixing, I found that previous coder used Appsetting to store information, which polls some static information from the system and nothing customization saved at run time, can be restored in the next run of the system. So, I came to plan to save the information in the xml file. Here I am going to show several things like


1. Creating xml file dynamically
2. Modifying the xml file
3. Reading from the xml file.

Creating XML file dynamically

//code for creating and saving default config
public void create_config_xml_file()
{
try {
//xml doc
XmlDocument xmldoc = new XmlDocument();

//xml declaration
XmlDeclaration xmldeclaration = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);

//create root element
XmlElement rootNode = xmldoc.CreateElement("myconfigdata");
xmldoc.InsertBefore(xmldeclaration, xmldoc.DocumentElement);
xmldoc.AppendChild(rootNode);

//adding more children to the root node

//imageformatindex
XmlElement ifiElement = xmldoc.CreateElement("imageformatindex");
ifiElement.InnerText = "1";
rootNode.AppendChild(ifiElement);

//currentdirectory
XmlElement currDirElement = xmldoc.CreateElement("currentdirectory");
currDirElement.InnerText = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
rootNode.AppendChild(currDirElement);

//fileprefix
XmlElement fpElement = xmldoc.CreateElement("fileprefix");
fpElement.InnerText = "ISeeStand";
rootNode.AppendChild(fpElement);

//filesuffixid
XmlElement fsElement = xmldoc.CreateElement("filesuffixid");
fsElement.InnerText = "2";
rootNode.AppendChild(fsElement);

//sequential
XmlElement seqElement = xmldoc.CreateElement("sequential");
seqElement.InnerText = "0";
rootNode.AppendChild(seqElement);

//sequentialvid
XmlElement seqVidElement = xmldoc.CreateElement("sequentialvid");
seqVidElement.InnerText = "0";
rootNode.AppendChild(seqVidElement);

//videncoder
XmlElement videncElement = xmldoc.CreateElement("videncoder");
videncElement.InnerText = "WMVideo9 Encoder DMO";
rootNode.AppendChild(videncElement);

//framesize
XmlElement fsizeElement = xmldoc.CreateElement("framesize");
fsizeElement.InnerText = "800 x 600 @5.00 fps";
rootNode.AppendChild(fsizeElement);

//framerate
XmlElement frateElement = xmldoc.CreateElement("framerate");
frateElement.InnerText = "5.00 fps";
rootNode.AppendChild(frateElement);

//getting appdata folder
string app_path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

//saving that document
xmldoc.Save(app_path+"/"+"enstatics.xml");

}
catch (Exception exc) {
MessageBox.Show("Failed to create the xml configuration file."+exc.Message);
}
}

From the above code, we can see how to use XMLDocument object and XmlNode for creating a XML file.

Modifying the XML file

The following code saves some info into the xml file accessing a particular XmlNode


//saving the image format index to the xml file
try {
XmlDocument xmldoc = MyStaticMgr.loadMyXMLDoc();
XmlNode formatIndexNode = xmldoc.GetElementsByTagName("imageformatindex")[0];
//setting the preferences to the xml node
formatIndexNode.InnerText = (ImageformatComboBox.SelectedIndex + 1).ToString();
//now saving the xml file
int result=MyStaticMgr.saveMyXMLDoc(xmldoc);
}
catch (Exception exc) {
MessageBox.Show("Failed to save the preferences"+exc.Message);
}

Reading from XML file

The following code will demonstrate how to access a particular node of the xml file for a particular information

//making a default choice
try {

XmlDocument xmldoc = MyStaticMgr.loadMyXMLDoc();
XmlNode formatIndexNode = xmldoc.GetElementsByTagName("imageformatindex")[0];
int format_index = int.Parse(formatIndexNode.InnerText);
//now setting that index to the combobox
ImageformatComboBox.SelectedIndex = format_index - 1;
}
catch (Exception exc) {
string messageBoxText = exc.Message;
string caption = "Exception Occurred";
MessageBox.Show(messageBoxText, caption);
}


From the above codes we found a particular section like this

XmlDocument xmldoc = MyStaticMgr.loadMyXMLDoc();

and

//now saving the xml file
int result=MyStaticMgr.saveMyXMLDoc(xmldoc);

Actually, MyStaticMgr is a static class which handles the basic operation like opening an xml document and returning the document object or saving the xmldocument object. Here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace ISeeStand
{
public static class MyStaticMgr
{

//code for loading the xml document
public static XmlDocument loadMyXMLDoc()
{
XmlDocument xmldoc = new XmlDocument();
string xmlfilepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//loading the file
xmldoc.Load(xmlfilepath + "/" + "enstatics.xml");
//returning the xmldoc
return xmldoc;

}

//code for saving to the xml file
public static int saveMyXMLDoc(XmlDocument xmldoc)
{
int saved = 0;
try {
string xmlfilepath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//loading the file
xmldoc.Save(xmlfilepath + "/" + "enstatics.xml");
//yes the file is saved
saved = 1;
}
catch (Exception exc) {
string messageBoxText = exc.Message;
string caption = "Exception Occurred";
MessageBox.Show(messageBoxText, caption);
}

//returning the result
return saved;

}



}
}

No comments:

Post a Comment