Nov 13, 2009

Working with XML Files

Working with XML files

In this tutorial, we will use the XmlDocument class to create and load XML files. You will use the XmlNode and XmlAttribute classes to build nodes. You will search for nodes by using the GetElementByName and SelectNodes methods, the last one using XPath.
Namespace Used
using System.Xml;
















protected void btncreatexml_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode xnDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(xnDeclaration);
XmlNode xnMenus = xmlDoc.CreateElement("Menus");
xmlDoc.AppendChild(xnMenus);
XmlNode xnMenuItem = xmlDoc.CreateElement("MenuItem");
XmlAttribute xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Home";
xnMenuItem.Attributes.Append(xaTitle);
XmlAttribute xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "Home.htm";
xnMenuItem.Attributes.Append(xaLink);
xnMenus.AppendChild(xnMenuItem);
//Node
xnMenuItem = xmlDoc.CreateElement("MenuItem");
xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Members";
xnMenuItem.Attributes.Append(xaTitle);
xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "Members.htm";
xnMenuItem.Attributes.Append(xaLink);
xnMenus.AppendChild(xnMenuItem);
//more nodes
XmlNode xnSubMenu = xmlDoc.CreateElement("MenuItem");
xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Add Member";
xnSubMenu.Attributes.Append(xaTitle);
xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "AddMember.htm";
xnSubMenu.Attributes.Append(xaLink);
xnMenuItem.AppendChild(xnSubMenu);
//more nodes
xnSubMenu = xmlDoc.CreateElement("MenuItem");
xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Delete Member";
xnSubMenu.Attributes.Append(xaTitle);
xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "deleteMember.htm";
xnSubMenu.Attributes.Append(xaLink);
xnMenuItem.AppendChild(xnSubMenu);
//
xnMenus.AppendChild(xnMenuItem);
//
//Adding another menu
xnMenuItem = xmlDoc.CreateElement("MenuItem");
xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Schedule";
xnMenuItem.Attributes.Append(xaTitle);
xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "Schedule.htm";
xnMenuItem.Attributes.Append(xaLink);
xnMenus.AppendChild(xnMenuItem);
//more nodes
xnSubMenu = xmlDoc.CreateElement("MenuItem");
xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Add Time";
xnSubMenu.Attributes.Append(xaTitle);
xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "AddTime.htm";
xnSubMenu.Attributes.Append(xaLink);
xnMenuItem.AppendChild(xnSubMenu);
//more nodes
xnSubMenu = xmlDoc.CreateElement("MenuItem");
xaTitle = xmlDoc.CreateAttribute("Title");
xaTitle.Value = "Delete Time";
xnSubMenu.Attributes.Append(xaTitle);
xaLink = xmlDoc.CreateAttribute("Link");
xaLink.Value = "deleteTime.htm";
xnSubMenu.Attributes.Append(xaLink);
xnMenuItem.AppendChild(xnSubMenu);
//
xnMenus.AppendChild(xnMenuItem);
//
xmlDoc.Save(Request.PhysicalApplicationPath + "\\XMLMenu.xml");
}
Next, you will add the code to generate a tree-like menu using the XML file that you created.
Double-click on the XML Menu button.
protected void btnxmlmenu_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Request.PhysicalApplicationPath + "XMLMenu.xml");
//GetElementsByTagName will return all parent and child nodes matching the tag name
XmlNodeList xnl = xmlDoc.GetElementsByTagName("MenuItem");
PlaceHolder1.Controls.Add(new LiteralControl(""));
foreach (XmlNode menuItem in xnl)
{
if (menuItem.ParentNode.Name == "Menus")
{
PlaceHolder1.Controls.Add(new LiteralControl("

"));
}
else
{
PlaceHolder1.Controls.Add(new LiteralControl("

"));
}
PlaceHolder1.Controls.Add(new LiteralControl("
"Link"].Value + ">" + menuItem.Attributes["Title"].Value + "
"Link"].Value + ">" + menuItem.Attributes["Title"].Value + "
"));
}
}
The last thing is how to search through nodes by using XPath. Using the SelectNodes method of the XmlDocument object, you will pass to the parameter of this method the path to the node through a textbox.
It will return all MenuItem nodes at the level: /Menus/MenuItem
Click on the Search w/ XPath button.
protected void btnsearch_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Request.PhysicalApplicationPath + "XMLMenu.xml");
XmlNodeList xnl = xmlDoc.SelectNodes(TextBox1.Text);
foreach (XmlNode n in xnl)
{
Response.Write(n.Attributes["Title"].Value + "
"
);
}
}

Bookmark and Share