您好,登錄后才能下訂單哦!
自己根據(jù)網(wǎng)上現(xiàn)有的代碼,改寫而成的。
生成XML文件的代碼:
/// <summary>
///
/// </summary>
/// <param name="dt">需要傳的參數(shù)</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">數(shù)據(jù)源</param>
public static void exportToXml(string filename, DataTable dt)
{
StreamWriter sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
//寫文件頭部?jī)?nèi)容
//下面是生成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>");
}
}
}
//寫文件尾部?jī)?nèi)容
sr.WriteLine("</VoteInfo>");
sr.WriteLine("</Vote>");
sr.Close();
}
讀取xml文件的代碼:
/// <summary>
/// 獲取xml中的數(shù)據(jù)()
/// </summary>
/// <param name="xdata">x軸數(shù)據(jù)</param>
/// <param name="ydata">y軸數(shù)據(jù)</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']");//讀取數(shù)據(jù)源
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);
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。