asp.net实现输出xml的方法
程序员文章站
2023-12-21 11:02:10
本文实例讲述了asp.net实现输出xml的方法。分享给大家供大家参考,具体如下:
#region 根据xml节点 查询xml节点的值并作修改
/// <...
本文实例讲述了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程序设计有所帮助。