Monday, June 24, 2013

Adding attributes to an XML node in c#

XmlDocument doc = new XmlDocument();
//Create Root Element
XmlElement root = doc.CreateElement("Datasource");
//Create Var Tag Node
XmlElement var = doc.CreateElement("var");
//Apply Host Name Attribute to the var element
var.SetAttribute("name", "Host");
// create innertext element for var
XmlElement str = doc.CreateElement("string");
str.InnerText = "127.0.0.1";
//add inntertext element to the var node
var.AppendChild(str);
//add var node in root node
root.AppendChild(var);

var = doc.CreateElement("var");
var.SetAttribute("name", "Database");
str = doc.CreateElement("string");
str.InnerText = "TestDB";
var.AppendChild(str);
root.AppendChild(var);

var = doc.CreateElement("var");
var.SetAttribute("name", "Username");
str = doc.CreateElement("string");
str.InnerText = "Root";
var.AppendChild(str);
root.AppendChild(var);

var = doc.CreateElement("var");
var.SetAttribute("name", "Password");
str = doc.CreateElement("string");
str.InnerText = "Root123";
var.AppendChild(str);
root.AppendChild(var);
doc.AppendChild(root);
//Create Xml File To Application Start up Path Folder
doc.Save(Application.StartupPath + "_datasource.xml");

9 comments:

  1. Replies
    1. Ankur Bhargav'S C Bolg >>>>> Download Now

      >>>>> Download Full

      Ankur Bhargav'S C Bolg >>>>> Download LINK

      >>>>> Download Now

      Ankur Bhargav'S C Bolg >>>>> Download Full

      >>>>> Download LINK Fv

      Delete