生成xml文件

来源:互联网 发布:调色软件app 编辑:程序博客网 时间:2024/06/04 18:00
写咯一个简单的 ,其实复杂的原理都一样。
  private string GenerateFile()        {            string path = null;            try            {                XmlDataDocument xmlDoc = new XmlDataDocument();                XmlNode xmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);                xmlDoc.AppendChild(xmlDocNode);                XmlNode node = xmlDoc.CreateElement("姓名");                xmlDoc.AppendChild(node);                XmlNode a = xmlDoc.CreateElement("年龄");                a.InnerXml = "21";                node.AppendChild(a);                XmlNode b = xmlDoc.CreateElement("身高体重");                node.AppendChild(b);                XmlNode c = xmlDoc.CreateElement("身高");                c.InnerXml = "168cm";                b.AppendChild(c);                XmlNode d = xmlDoc.CreateElement("体重");                d.InnerXml = "55kg";                b.AppendChild(d);                string policyfilename = System.DateTime.Now.ToString("yyyymmdd") + ".xml";                path = System.Web.HttpContext.Current.Server.MapPath("/xml") + @"\" + policyfilename;                xmlDoc.Save(path);                OutputFile(path);            }            catch (Exception ex)            {                path = null;            }            return path;        }

实现下载:

private void OutputFile(string generatedFileName)        {            string type = ".xml";            FileInfo file = new FileInfo(generatedFileName);            Response.Clear();            Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpContext.Current.Server.UrlEncode(file.Name));            Response.AddHeader("Content-Length", file.Length.ToString());            Response.ContentType = type;            Response.WriteFile(file.FullName);            //Response.End();        }


效果:
<?xml version="1.0" encoding="UTF-8"?>-<姓名>      <年龄>21</年龄>     -<身高体重>         <身高>168cm</身高>         <体重>55kg</体重>      </身高体重> </姓名>