您好,登錄后才能下訂單哦!
自己根據網上現有的代碼,改寫而成的。
生成XML文件的代碼:
/// <summary>
///
/// </summary>
/// <param name="dt">需要傳的參數</param>
public static void getXml(DataTable dt)
{
string path = System.Web.HttpContext.Current.Server.MapPath("~/XML//");//生成文件所在的位置
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += "Plant.xml";//文件名稱
exportToXml(path, dt);
}
/// <summary>
/// 生成XML文件
/// </summary>
/// <param name="filename">文件路徑</param>
/// <param name="dt">數據源</param>
public static void exportToXml(string filename, DataTable dt)
{
StreamWriter sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
//寫文件頭部內容
//下面是生成RSS的OPML文件
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sr.WriteLine("<Vote>");
sr.WriteLine("<VoteInfo><ID>1</ID><VoteTitle></VoteTitle>");
int i = 0;
if (dt.Rows.Count > 0)
{
for (int k = 0; k < dt.Rows.Count;k++ )
{
i++;
if (k == 0)
{
sr.WriteLine("<Item>");
sr.WriteLine("<VoteID>" + i + "</VoteID>");
sr.WriteLine("<Title>" + Convert.ToDateTime(dt.Rows[k]["Addtime"].ToString()).ToLongDateString() + "</Title>");
string shen = "";
string unit = "";
string m = dt.Rows[k]["Height"].ToString();
Regex reg = new Regex(@"[^0-9,^.]+");
Regex reg2 = new Regex(@"[^A-Za-z]+");
shen = reg.Replace(m, "");
unit = reg2.Replace(m, "");
sr.WriteLine("<Unit>"+unit+"</Unit>");
sr.WriteLine("<Count>" + shen + "</Count>");
sr.WriteLine("</Item>");
}
else
{
sr.WriteLine("<Item>");
sr.WriteLine("<VoteID>" + i + "</VoteID>");
sr.WriteLine("<Title>" + Convert.ToDateTime(dt.Rows[k-1]["Updatetime"].ToString()).ToLongDateString() + "</Title>");
string shen = "";
string unit = "";
string m = dt.Rows[k]["Height"].ToString();
Regex reg = new Regex(@"[^0-9,^.]+");
Regex reg2 = new Regex(@"[^A-Za-z]+");
shen = reg.Replace(m, "");
unit = reg2.Replace(m, "");
sr.WriteLine("<Unit>" + unit + "</Unit>");
sr.WriteLine("<Count>" + shen + "</Count>");
sr.WriteLine("</Item>");
}
}
}
//寫文件尾部內容
sr.WriteLine("</VoteInfo>");
sr.WriteLine("</Vote>");
sr.Close();
}
讀取xml文件的代碼:
/// <summary>
/// 獲取xml中的數據()
/// </summary>
/// <param name="xdata">x軸數據</param>
/// <param name="ydata">y軸數據</param>
/// <param name="unit">單位</param>
public static void getXML(string xdata, string ydata, string unit)
{
XmlDocument myDoc = new XmlDocument();
myDoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/XML//Plant.xml"));
XmlNode xn = myDoc.SelectSingleNode("//VoteInfo[ID='1']");//讀取數據源
XmlNodeList xnl = xn.SelectNodes("Item");
for (int i = 0; i < xnl.Count; i++)
{
XmlNode xn0 = xnl.Item(i);
ydata += xn0.SelectSingleNode("Count").InnerText + ",";
xdata += "\'" + xn0.SelectSingleNode("Title").InnerText + "\'" + ",";
unit = xn0.SelectSingleNode("Unit").InnerText;
}
ydata = ydata.Remove(ydata.Length - 1, 1);
xdata = xdata.Remove(xdata.Length - 1, 1);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。