自己根据网上现有的代码,改写而成的。
生成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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。