Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

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 + "
"
);
}
}

Oct 21, 2009

Bind XML to DataGrid

Binding XML to Datagrid :

In this code we’ll create XML and bind it to Datagrid control.
  1. Add XML file to project (By right click on project à Add à Add New Item à XML file)
2. Now create a XML file (Here I have created a file called Students.xml

<students>
<student>
<id>S0001</id>
<name>Deepika</name>
<city>Ahmedabad</city>
</student>
<student>
<id>S0002</id>
<name>Swati</name>
<city>Maninagar</city>
</student>
<student>
<id>S0003</id>
<name>Dhwani</name>
<city>Gandhinagar</city>
</student>
<student>
<id>S0004</id>
<name>Shweta</name>
<city>Rajkot</city>
</student>
</students>
3. Notice that when you have an XML file opened, the XML menu appears.
Click on the XML menu. The Create Schema will provide a graphic interface to draw the schema for the XML file. The Validate XML Schema helps you with determining if the XML content validates against a certain XML schema.
4. VS.NET tries to come up with the schema for the XML file based on its content. You can open up the schema file and graphically edit or change its definition by code.
Double-click on the Students.xsd file.





  1. By default, the Create XML Schema option defines all the fields as String. You can change it in this GUI or directly in the code representing this schema.
  2. Now add a button called “Load XML” to a webform which will populate data to gridview from xml file.
protected void btnload_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet("Students");
ds.ReadXmlSchema(Server.MapPath(".") + "\\Students.xsd");
ds.ReadXml(Server.MapPath(".") + "\\Students.xml");
GridView1.DataSource = ds;
GridView1.DataBind();
}