This is complete project
How will it works.
1) This code contain master page so just remove it if u don't need.
2) All the xml files will show in a Table in page XmlFiles.aspx.
3) Select File that you want to open.after selection selected file will open in page 2.
4) System.Configuration.ConfigurationManager.AppSettings["configFiles"] contain folder path where xml files exist.
This page will show all xml files in a table.
.aspx page
<%@ Page Language="C#" MasterPageFile="~/AuditMaster.master" AutoEventWireup="true"
CodeFile="XmlFiles.aspx.cs" Inherits="Admin_XmlFiles" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div id="contener">
<div>
<span style="color: #ff99ff">
<br />
<span style="color:Blue">List of Availble XML files :<br />
<br />
</span></span>
</div>
<table border="1" style="font-size: 12pt">
<tr>
<th style=" font-size:10pt">
Serial No.</th>
<th style=" font-size:10pt">
File Name</th>
</tr>
<%int i = 0; %>
<%foreach (System.IO.FileInfo fi in rgFiles)
{%>
<tr>
<td style=" font-size:10pt">
<%=++i%>
</td>
<td style=" font-size:10pt">
<a href="ServiceDashboardManager.aspx?fileName=<%=fi.Name %>">
<%=fi.Name%>
</a>
</td>
</tr>
<%} %>
</table>
</div>
</asp:Content>
How will it works.
1) This code contain master page so just remove it if u don't need.
2) All the xml files will show in a Table in page XmlFiles.aspx.
3) Select File that you want to open.after selection selected file will open in page 2.
4) System.Configuration.ConfigurationManager.AppSettings["configFiles"] contain folder path where xml files exist.
This page will show all xml files in a table.
.aspx page
<%@ Page Language="C#" MasterPageFile="~/AuditMaster.master" AutoEventWireup="true"
CodeFile="XmlFiles.aspx.cs" Inherits="Admin_XmlFiles" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div id="contener">
<div>
<span style="color: #ff99ff">
<br />
<span style="color:Blue">List of Availble XML files :<br />
<br />
</span></span>
</div>
<table border="1" style="font-size: 12pt">
<tr>
<th style=" font-size:10pt">
Serial No.</th>
<th style=" font-size:10pt">
File Name</th>
</tr>
<%int i = 0; %>
<%foreach (System.IO.FileInfo fi in rgFiles)
{%>
<tr>
<td style=" font-size:10pt">
<%=++i%>
</td>
<td style=" font-size:10pt">
<a href="ServiceDashboardManager.aspx?fileName=<%=fi.Name %>">
<%=fi.Name%>
</a>
</td>
</tr>
<%} %>
</table>
</div>
</asp:Content>
.Cs Page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using TboCommon;
public partial class Admin_XmlFiles : System.Web.UI.Page
{
protected FileInfo[] rgFiles = new FileInfo[0];
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "View XML files";
string configPath = System.Configuration.ConfigurationManager.AppSettings["configFiles"];
DirectoryInfo di = new DirectoryInfo(configPath);
rgFiles = di.GetFiles("*.xml");
}
}
Page 2--------after click on xml file of first page this page will open.
.aspx page
<%@ Page Language="C#" MasterPageFile="~/AuditMaster.master" AutoEventWireup="true"
CodeFile="ServiceDashboardManager.aspx.cs" Inherits="Admin_ServiceDashboardManager"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div id="contener">
<div style="margin-top: 20px; margin-left: 5px;">
<asp:XmlDataSource ID="XmlDataSource1" runat="server" />
<asp:TreeView ID="treeView1" ExpandDepth="3" MaxDataBindDepth="3" runat="server">
</asp:TreeView>
<div style="margin-top: 10px;">
<asp:Button ID="Save" runat="server" Text="Save" OnClick="Save_Click" />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>
</div>
</div>
</asp:Content>
.cs page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using TboCommon;
public partial class Admin_ServiceDashboardManager : System.Web.UI.Page,IAuthentication
{
protected string fileName = string.Empty;
protected string physicalPath = ConfigurationManager.AppSettings["configFiles"];
string applicationname = string.Empty;
protected XmlTextReader reader;
protected XmlNodeType nType = new XmlNodeType();
int counter = 0;
int tempcount = 0;
static StreamWriter sr;
protected string errorMessage = string.Empty;
XmlDocument doc;
int memberId = 0;
string fullPath = string.Empty;
protected FileInfo[] rgFiles = new FileInfo[0];
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Update XML file";
string configPath = System.Configuration.ConfigurationManager.AppSettings["configFiles"];
DirectoryInfo di = new DirectoryInfo(configPath);
string s = Convert.ToString(Request.QueryString["fileName"]);
if (s != null && s.Length > 2)
{
fileName = s;
showxml(fileName);
}
else
{
}
}
void showxml(string fileName)
{
fullPath = physicalPath + fileName;
XmlDataSource1.DataFile = fullPath;
try
{
treeView1.Nodes.Clear();
// Loads the XML document
reader = new XmlTextReader(fullPath);
doc = new XmlDocument();
doc.Load(fullPath);
// Adds the root to the treeview
treeView1.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
// Adds remainings nodes to the tree view
AddNode(doc.DocumentElement, tNode);
// collpasing all the Nodes.
treeView1.CollapseAll();
}
catch (XmlException xmlEx)
{
string IPAddr = Request.ServerVariables["REMOTE_ADDR"];
Response.Write(xmlEx.Message);
}
catch (Exception ex)
{
string IPAddr = Request.ServerVariables["REMOTE_ADDR"];
Response.Write(ex.Message);
}
}
/// <summary>
/// This Method will add a node recursively and construct Tree View
/// </summary>
/// <param name="inXmlNode"></param>
/// <param name="inTreeNode"></param>
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
HtmlInputText tempText = new HtmlInputText();
// If it is having child nodes then it will add them recursively
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddNode(xNode, tNode);
}
}
else if (inXmlNode.InnerText.Equals(""))
{
// If the element doesnt contain any data Item then display empty Text Box
inTreeNode.ChildNodes.Add(new TreeNode(inXmlNode.Name));
inTreeNode.ChildNodes[0].Text = "<input type='text' name='" + counter + "' value=''>";
counter++;
}
else
{ // if it is Data then display it in Text box.
inTreeNode.Text = "<input type='text' name='" + counter + "' value='" + (inXmlNode.OuterXml.Trim()) + "'>";
counter++;
}
}
/// <summary>
/// This method will update the XML when user saves the data.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Save_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
exportToXml(treeView1, fullPath);
}
Response.Redirect("ServiceDashboardManager.aspx?fileName=" + fileName);
}
string str = string.Empty;
/// <summary>
/// This method will overwrites the existing XML
/// </summary>
/// <param name="tv"></param>
/// <param name="filename"></param>
public void exportToXml(TreeView tv, string fullPath)
{
sr = new StreamWriter(fullPath, false, System.Text.Encoding.UTF8);
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
IEnumerator ie = tv.Nodes.GetEnumerator();
if (ie.MoveNext())
{
TreeNode tn = (TreeNode)ie.Current;
sr.WriteLine("<" + tn.Text + ">");
parseNode(tn);
}
sr.Close();
}
/// <summary>
/// This Method will update the XML file recursively.
/// </summary>
/// <param name="tn"></param>
private void parseNode(TreeNode tn)
{
IEnumerator ie = tn.ChildNodes.GetEnumerator();
string parentnode = "";
int flag = 0;
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
// if it is element write it in file.
if (ctn.ChildNodes.Count != 0)
{
sr.Write("<" + ctn.Text + ">");
}
// if it is data item then get the latest info from text boxes
if (ctn.ChildNodes.Count == 0 && flag == 0)
{
string count = tempcount.ToString();
string value = Request[count];
sr.Write(value);
tempcount++;
}
flag = 1;
// if it is having child nodes then add them recursively
if (ctn.ChildNodes.Count > 0)
{
parseNode(ctn);
}
}
sr.Write("</" + parentnode + ">");
sr.WriteLine("");
}
}
No comments:
Post a Comment