XMLbySQL

VisualER

OpenByText

WebDBView

 

 

 

Исходный код

XMLbySQL создан с использованием Visual Studio 2003.

Благодаря входящему в .NET Framework набору классов для работы с XML и базами данных код не содержит каких-то сложных элементов и достаточно прост для анализа.

default.aspx

<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="xmlbysql.Engine" validateRequest=false%>

default.aspx.cs

using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Mail;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
namespace xmlbysql
{
public class Engine : System.Web.UI.Page
{
XmlDocument query=new System.Xml.XmlDocument();
XmlDocument commands=new System.Xml.XmlDocument();
XmlNode node;
NameValueCollection parameters=new NameValueCollection();
String binding=String.Empty;
String pageID=String.Empty;
String content="";
String cantLoad;
String bindingName="...";
private void Page_Load(object sender, System.EventArgs e)
{
bool flag=true;
Response.Cache.SetNoServerCaching();
Response.Expires=0;
XmlDocument doc=new XmlDocument();
XslTransform xslt=new XslTransform();
createVariables();
if (loadQuery())
{
setVariables();
commands.LoadXml(query.OuterXml);
node=commands.DocumentElement;
XsltArgumentList args=new XsltArgumentList();
if (commands.SelectSingleNode("commands/binding")!=null)
{
try
{
bindingName=Server.MapPath(commands.SelectSingleNode("commands/binding").InnerText);
doc.Load(bindingName);
xslt.Load(doc,new XmlUrlResolver(),null);
doc.LoadXml(commands.OuterXml);
foreach(String i in parameters.AllKeys)
{
args.AddParam(i,"",parameters[i]);
}
commands.Load(xslt.Transform(doc,args,new XmlUrlResolver()));
commands.LoadXml("<?xml version=\"1.0\" ?>"+commands.OuterXml);
}
catch (Exception ex)
{
Response.Write("<?xml version=\"1.0\" ?>\n"
+"<resultset><error>\n"
+safeString(ex.ToString())
+"\n</error></resultset>"
);
flag=false;
}
}
if (flag)
{
execute(commands);
}
}
else
{
content="<?xml version=\"1.0\" ?><error>\nCan not load "+pageID+".xml\n";
content=content+"<message>\n"+safeString(cantLoad)+"\n</message></error>";
Response.Write(content);
}
}
void checkPageName()
{
if ((pageID==null) || (pageID.Length<1))
{
pageID="index";
}
}
bool loadQuery()
{
bool flag=true;
try
{
query.Load(Server.MapPath(pageID+".xml"));
}
catch (Exception e)
{
cantLoad=e.ToString();
flag=false;
}
return flag;
}
void insertCookieVariables()
{
foreach(String i in Request.Cookies)
{
insertVariable(i,Request.Cookies[i].Value);
}
}
void insertGetVariables()
{
foreach(String i in Request.QueryString)
{
insertVariable(i,HttpUtility.UrlDecode(Request.QueryString[i]));
}
}
void insertPostVariables()
{
foreach(String i in Request.Form)
{
insertVariable(i,Request.Form[i]);
}
}
void insertSessionVariables()
{
foreach(String i in Page.Session.Keys)
{
insertVariable(i,Page.Session[i].ToString());
}
}
void insertGlobalVariables()
{
foreach(String i in System.Configuration.ConfigurationSettings.AppSettings)
{
insertVariable(i,System.Configuration.ConfigurationSettings.AppSettings[i].ToString());
}
}
void setVariables()
{
String vars="";
Page.Session.RemoveAll();
foreach(String i in parameters.AllKeys)
{
Page.Session.Add(i,parameters[i]);
vars=vars+i+"="+parameters[i]+";";
}
}
void createVariables()
{
insertGetVariables();
insertPostVariables();
insertCookieVariables();
insertSessionVariables();
insertGlobalVariables();
checkPageName();
insertVariable("page",pageID);
}
void insertVariable(String varName, String varValue)
{
if (varName!=null)
{
if (parameters[varName]==null)
{
if (varName=="page")
{
pageID=varValue;
}
try
{
parameters[XmlConvert.VerifyName(varName)]=varValue;
}
catch{}
}
}
}
void execute(XmlDocument cmds)
{
XmlNode transform=null;
String stylefile=null;
String mime=null;
XmlDocument doc=new XmlDocument();
XslTransform xslt=new XslTransform();
String xml="<?xml version=\"1.0\" ?>";
if (cmds.SelectSingleNode("commands/style")!=null)
{
xml=xml+"\n<?xml-stylesheet type=\"text/xsl\" href=\""+cmds.SelectSingleNode("commands/style").InnerText+"\" ?>";
transform=cmds.SelectSingleNode("commands/style/@transform");
try
{
mime=cmds.SelectSingleNode("commands/style/@mime").InnerText;
}
catch {}
stylefile=cmds.SelectSingleNode("commands/style").InnerText;
}
xml=xml+"\n<resultset>";
String data="";
try
{
XmlNodeList elemList=cmds.GetElementsByTagName("command");
for (int i=0; i < elemList.Count; i++)
{
data=data+executeCommand(elemList[i]);
}
xml=xml+data;
}
catch (Exception e)
{
xml=xml+"\n<error>"+e.Message+"</error>";
}
xml=xml+"\n</resultset>";
if (transform!=null)
{
if (transform.InnerText=="server")
{
try
{
if (mime!=null)
{
Response.ContentType=mime;
}
doc.Load(Server.MapPath(stylefile));
xslt.Load(doc,new XmlUrlResolver(),null);
doc.LoadXml(xml);
xslt.Transform(doc,null,Response.OutputStream,new XmlUrlResolver());
}
catch (Exception ex)
{
Response.Write("<?xml version=\"1.0\" ?>\n"
+"<resultset><error>\n"
+safeString(ex.ToString())
+"\n</error></resultset>"
);
}
}
else
{
Response.Write(xml);
}
}
else
{
Response.Write(xml);
}
}
String safeString(String s)
{
s=s.Replace("&","&amp;");
s=s.Replace("<","&lt;");
s=s.Replace(">","&gt;");
s=s.Replace("\"","&quot;");
s=s.Replace("'","&apos;");
return s;
}
String unsafeString(String s)
{
String v;
XmlDocument d=new XmlDocument();
try
{
d.LoadXml(s);
v=d.OuterXml;
}
catch
{
v=safeString(s);
}
return v;
}
String nativeString(String s)
{
s=s.Replace("&lt;","<");
s=s.Replace("&gt;",">");
s=s.Replace("&quot;","\"");
s=s.Replace("&apos;","'");
s=s.Replace("&amp;","&");
return s;
}
String executeCommand(XmlNode cmd)
{
String xml="";
switch (cmd.SelectSingleNode("connection").Attributes["type"].Value)
{
case "folder":
xml=xml+executeCommand_folder(cmd);
break;
case "dir":
xml=xml+executeCommand_dir(cmd);
break;
case "empty":
xml=xml+executeCommand_empty(cmd);
break;
case "oledb":
xml=xml+executeCommand_oledb(cmd);
break;
case "odbc":
xml=xml+executeCommand_odbc(cmd);
break;
case "web":
xml=xml+executeCommand_web(cmd);
break;
case "mail":
xml=xml+executeCommand_mail(cmd);
break;
default:
xml=xml+executeCommand_unknown(cmd);
break;
}
return xml;
}
String executeCommand_mail(XmlNode cmd)
{
String xml="";
String data="";
String noconvert="";
String type="";
String one;
MailMessage msg;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
SmtpMail.SmtpServer=cmd.SelectSingleNode("connection").InnerText;
for (int i=0;i<elemList.Count;i++)
{
noconvert="";
try
{
noconvert=elemList[i].Attributes["unsafe"].Value;
}
catch
{
}
msg=new MailMessage();
msg.From=elemList[i].Attributes["from"].Value;
msg.To=elemList[i].Attributes["to"].Value;
msg.Subject=elemList[i].Attributes["subject"].Value;
try
{
type=elemList[i].Attributes["type"].Value;
}
catch {}
if (type=="html")
{
msg.BodyFormat=MailFormat.Html;
}
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one="";
try
{
one=one+"\n<columns><column name=\"mail\" /></columns>";
one=one+"\n<records>";
one=one+"\n<row>";
one=one+"<cell name=\"mail\">ok";
if (noconvert=="yes")
{
msg.Body=unsafeString(elemList[i].InnerXml);
}
else
{
msg.Body=safeString(elemList[i].InnerText);
}
SmtpMail.Send(msg);
one=one+"</cell>";
one=one+"\n</row>";
one=one+"\n</records>";
data=data+one;
}
catch (Exception e)
{
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_odbc(XmlNode cmd)
{
String xml="";
String data="";
String one;
String noconvert;
OdbcConnection cn;
OdbcCommand sql;
OdbcDataReader rdr;
int fc;
bool flag;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
#region command
cn=new OdbcConnection(nativeString(cmd.SelectSingleNode("connection").InnerText));
cn.Open();
for (int i=0;i<elemList.Count;i++)
{
try
{
#region statement
one="";
sql=new OdbcCommand(nativeString(elemList[i].InnerText),cn);
rdr=sql.ExecuteReader();
flag=true;
while (flag)
{
noconvert="";
try
{
noconvert=elemList[i].Attributes["unsafe"].Value;
}
catch
{
}
one=one+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one=one+"\n<columns>";
for (fc=0;fc<rdr.FieldCount;fc++)
{
one=one+"<column name=\""+rdr.GetName(fc)+"\"/>";
}
one=one+"\n</columns>";
one=one+"\n<records>";
while (rdr.Read())
{
one=one+"\n<row>";
for (fc=0;fc<rdr.FieldCount;fc++)
{
one=one+"<cell name=\""+rdr.GetName(fc)+"\">";
if (noconvert=="yes")
{
one=one+unsafeString(rdr.GetValue(fc).ToString());
}
else
{
one=one+safeString(rdr.GetValue(fc).ToString());
}
one=one+"</cell>";
}
one=one+"\n</row>";
}
one=one+"\n</records>";
flag=rdr.NextResult();
if (flag)
{
one=one+"\n</statement>";
}
}
rdr.Close();
sql.Dispose();
data=data+one;
#endregion
}
catch (Exception e)
{
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
#endregion
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_oledb(XmlNode cmd)
{
String xml="";
String data="";
String one;
String noconvert;
OleDbConnection cn;
OleDbCommand sql;
OleDbDataReader rdr;
int fc;
bool flag;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
#region command
cn=new OleDbConnection(nativeString(cmd.SelectSingleNode("connection").InnerText));
cn.Open();
for (int i=0;i<elemList.Count;i++)
{
try
{
#region statement
one="";
sql=new OleDbCommand(nativeString(elemList[i].InnerText),cn);
rdr=sql.ExecuteReader();
flag=true;
while (flag)
{
noconvert="";
try
{
noconvert=elemList[i].Attributes["unsafe"].Value;
}
catch
{
}
one=one+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one=one+"\n<columns>";
for (fc=0;fc<rdr.FieldCount;fc++)
{
one=one+"<column name=\""+rdr.GetName(fc)+"\"/>";
}
one=one+"\n</columns>";
one=one+"\n<records>";
while (rdr.Read())
{
one=one+"\n<row>";
for (fc=0;fc<rdr.FieldCount;fc++)
{
one=one+"<cell name=\""+rdr.GetName(fc)+"\">";
if (noconvert=="yes")
{
one=one+unsafeString(rdr.GetValue(fc).ToString());
}
else
{
one=one+safeString(rdr.GetValue(fc).ToString());
}
one=one+"</cell>";
}
one=one+"\n</row>";
}
one=one+"\n</records>";
flag=rdr.NextResult();
if (flag)
{
one=one+"\n</statement>";
}
}
rdr.Close();
sql.Dispose();
data=data+one;
#endregion
}
catch (Exception e)
{
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
#endregion
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_folder(XmlNode cmd)
{
String xml="";
String data="";
String one;
FileInfo[] fiArr;
DirectoryInfo di;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
di=new DirectoryInfo(Server.MapPath(nativeString(cmd.SelectSingleNode("connection").InnerText)));
for (int i=0;i<elemList.Count;i++)
{
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one="";
try
{
fiArr=di.GetFiles(elemList[i].InnerText);
one=one+"\n<columns>\n<column name=\"file name\" />";
one=one+"<column name=\"file size\" />\n</columns>";
one=one+"\n<records>";
foreach (FileInfo fi in fiArr)
{
one=one+"\n<row>";
one=one+"<cell name=\"file name\">"+fi.Name+"</cell>";
one=one+"<cell name=\"file size\">"+fi.Length.ToString()+"</cell>";
one=one+"\n</row>";
}
one=one+"\n</records>";
data=data+one;
}
catch (Exception e)
{
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_dir(XmlNode cmd)
{
String xml="";
String data="";
String one;
DirectoryInfo[] dirs;
DirectoryInfo di;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
di=new DirectoryInfo(Server.MapPath(nativeString(cmd.SelectSingleNode("connection").InnerText)));
for (int i=0;i<elemList.Count;i++)
{
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one="";
try
{
dirs=di.GetDirectories(elemList[i].InnerText);
one=one+"\n<columns>\n<column name=\"folder name\" />";
one=one+"\n</columns><records>";
foreach (DirectoryInfo d in dirs)
{
one=one+"\n<row>";
one=one+"<cell name=\"folder name\">"+d.Name+"</cell>";
one=one+"\n</row>";
}
one=one+"\n</records>";
data=data+one;
}
catch (Exception e)
{
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_web(XmlNode cmd)
{
String xml="";
String data="";
String noconvert="";
String one;
WebRequest wr;
WebResponse rez;
StreamReader sr;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
for (int i=0;i<elemList.Count;i++)
{
noconvert="";
try
{
noconvert=elemList[i].Attributes["unsafe"].Value;
}
catch
{
}
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one="";
try
{
one=one+"\n<columns><column name=\"web\" /></columns>";
one=one+"\n<records>";
one=one+"\n<row>";
wr=WebRequest.Create(nativeString(elemList[i].InnerText));
rez=wr.GetResponse();
sr=new StreamReader(rez.GetResponseStream());
one=one+"<cell name=\"web\">";
if (noconvert=="yes")
{
one=one+unsafeString(sr.ReadToEnd());
}
else
{
one=one+safeString(sr.ReadToEnd());
}
one=one+"</cell>";
rez.Close();
wr.Abort();
one=one+"\n</row>";
one=one+"\n</records>";
data=data+one;
}
catch (Exception e)
{
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_empty(XmlNode cmd)
{
String xml="";
String data="";
String one;
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
try
{
for (int i=0;i<elemList.Count;i++)
{
data=data+"\n<statement name=\""+elemList[i].Attributes["name"].Value+"\">";
one="";
try
{
one=one+"\n<columns><column name=\"empty\" /></columns>";
one=one+"\n<records>";
one=one+"\n<row>";
one=one+"<cell name=\"empty\">"+elemList[i].InnerXml+"</cell>";
one=one+"\n</row>";
one=one+"\n</records>";
data=data+one;
}
catch (Exception e)
{
data=data+"\n<error>"+safeString(e.Message)+"</error>";
}
data=data+"\n</statement>";
}
xml=xml+data;
}
catch (Exception e)
{
xml=xml+"\n<error>"+safeString(e.Message)+"</error>";
}
xml=xml+"\n</command>";
return xml;
}
String executeCommand_unknown(XmlNode cmd)
{
String xml="";
XmlNodeList elemList=cmd.SelectNodes("statement");
xml=xml+"\n<command name=\""+cmd.Attributes["name"].Value+"\">";
xml=xml+"\n<error>unknown type: "+cmd.SelectSingleNode("connection").Attributes["type"].Value+"</error>";
xml=xml+"\n</command>";
return xml;
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}

 

 

 

   
sss1024@mail.ru  

                          Найти: на

 

Сайт создан в системе uCoz