1 - Create file XML.ashx
2 - Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace RDO.Cronograma
{
///
/// Summary description for XML1
///
public class XML1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string SQL;
MySqlDataReader rdr = null;
MySqlConnection con = null;
//Aloca a memoria para objeto de comando a ser enviado para o banco
MySqlCommand cmd = null;
string connectionString = ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;
con = new MySqlConnection(connectionString);
//clear cache
context.Response.CacheControl = "no-cache";
context.Response.AddHeader("Pragma", "no-cache");
context.Response.Expires = -1;
con.Open();
SQL = string.Format(@"
SELECT
id,
DATE_FORMAT(start_date,’%Y-%m-%d %h:%i-%s’) as start_date,
duration,
‘Teste’ as end_date,
text,
progress,
sortorder,
parent
FROM gantt_tasks
");
cmd = new MySqlCommand(SQL);
cmd.Connection = con;
//create tag <data>
XDocument xDocument = new XDocument(
new XElement("data"));
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
xDocument.Element("data").Add(
new XElement("task",
new XAttribute("id", rdr["id"].ToString()),
new XElement("start_date",
new XCData(rdr["start_date"].ToString())),
new XElement("duration",
new XCData(rdr["duration"].ToString())),
new XElement("end_date",
new XCData("")),
new XElement("text",
new XCData(rdr["text"].ToString())),
new XElement("progress",
new XCData(rdr["progress"].ToString())),
new XElement("sortorder",
new XCData(rdr["sortorder"].ToString())),
new XElement("parent",
new XCData(rdr["parent"].ToString())),
new XElement("open","1")
));
}
rdr.Close();
xDocument.Element("data").Add(
new XElement("coll_options", new XAttribute("for", "links")));
SQL = string.Format(@"
SELECT * FROM gantt_links
");
cmd = new MySqlCommand(SQL);
cmd.Connection = con;
rdr = cmd.ExecuteReader();
//create tag links
while (rdr.Read())
{
xDocument.Element("data").Element("coll_options").Add(
new XElement("item",
new XAttribute("source", rdr["source"].ToString()),
new XAttribute("target", rdr["target"].ToString()),
new XAttribute("type", rdr["type"].ToString())));
}
rdr.Close();
con.Close();
context.Response.ContentType = "text/xml";
context.Response.Charset = "UTF-8";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
xDocument.Save(context.Response.Output);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
javascript
gantt.load(“XML.ashx”, “xml”);