富贵资源网 Design By www.hznty.com

本文实例讲述了asp.net实现输出xml的方法。分享给大家供大家参考,具体如下:

#region 根据xml节点 查询xml节点的值并作修改
/// <summary>
/// 根据xml节点 查询xml节点的值并作修改
/// </summary>
/// <param name="fileName">要加载的xml文件</param>
/// <param name="tagName">xml节点的名称</param>
/// <param name="newXmlText">修改后的值</param>
/// <returns></returns>
public void GetXmlbySelNodel(string fileName, string selNodeName, string tagName,string newXmlText)
{
  try
  {
   doc.Load(Server.MapPath(fileName));
   XmlNodeList nodelist = doc.DocumentElement.SelectSingleNode(selNodeName).ChildNodes;
   foreach (XmlNode node in nodelist)
   {
    XmlElement xle = node as XmlElement;
    XmlNodeList list = xle.ChildNodes;
    for (int j = 0; j < list.Count; j++) {
     if (xle.ChildNodes[j].Name == tagName) {
      xle.ChildNodes[j].InnerText = newXmlText;
      SaveXml(fileName);
      break;
     }
    }
   }
  }
  catch { 
  }
}
#endregion
#region 保存修改后xml文件内容
//保存修改后xml文件内容
public void SaveXml(string fileName)
{
  doc.Save(Server.MapPath(fileName));
}
#endregion
string selNodeName="//url/data";
string fileName = "meituanAPI.xml";
GetXmlbySelNodel(fileName, selNodeName, "website", "");
GetXmlbySelNodel(fileName, selNodeName, "siteurl", "www.jb51.net");
GetXmlbySelNodel(fileName, selNodeName, "city", "杭州");
GetXmlbySelNodel(fileName, selNodeName, "tip", "送ipad 抽奖!!");
GetXmlbySelNodel(fileName, "//data/shops", "name", "上海洲信 信息技术有限公司!");
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("meituanAPI.xml"));
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
ds.Tables[0].WriteXml(writer);
Response.ContentType = "text/xml;charset=gb2312";
Response.Flush();
Response.End()

或者

Response.ClearContent();
Response.ClearHeaders();
Response.contentType="application/xml"';
Response.WriterFile(Server.Mappath("API/54tuanAPI.xml"));
Response.Flush();
Response.End();

希望本文所述对大家asp.net程序设计有所帮助。

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com