asp. net Implementation of Output xml

  • 2021-07-07 07:14:19
  • OfStack

In this paper, an example is given to describe the method of asp. net to output xml. Share it for your reference, as follows:


#region  According to xml Node   Query xml Node and modify the value of the 
/// <summary>
///  According to xml Node   Query xml Node and modify the value of the 
/// </summary>
/// <param name="fileName"> To load xml Documents </param>
/// <param name="tagName">xml The name of the node </param>
/// <param name="newXmlText"> Modified value </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  After saving the modifications xml File content 
// After saving the modifications xml File content 
public void SaveXml(string fileName)
{
  doc.Save(Server.MapPath(fileName));
}
#endregion
string selNodeName="//url/data";
string fileName = "meituanAPI.xml";
GetXmlbySelNodel(fileName, selNodeName, "website", " Script House ");
GetXmlbySelNodel(fileName, selNodeName, "siteurl", "www.ofstack.com");
GetXmlbySelNodel(fileName, selNodeName, "city", " Hangzhou ");
GetXmlbySelNodel(fileName, selNodeName, "tip", " Send ipad  Lucky draw !!");
GetXmlbySelNodel(fileName, "//data/shops", "name", " Shanghai Zhou Xin   Information technology co., ltd !");
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()

Or


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

I hope this paper is helpful to everyone's asp. net programming.


Related articles: