一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net生成xml文档并发送到客户端代码

时间:2022-06-25 06:37:07 编辑:袖梨 来源:一聚教程网

asp教程.net生成xml文档并发送到客户端代码
//2个参数,一个是网址,一个是参数 也就是你的xml

  public static string SendRequest(string uri, string poscontent)
  {
  string responseText = "";
  try
  {
  StreamReader reader;
//这里可以设置编码
  byte[] postBody = System.Text.Encoding.Default.GetBytes(poscontent);
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  request.Method = "POST";
  request.ContentType = "application/x-www-form-urlencoded";
  request.ContentLength = postBody.Length;
  request.AllowWriteStreamBuffering = true;

  Stream dataStream = request.GetRequestStream();
  dataStream.Write(postBody, 0, postBody.Length);
  dataStream.Close();
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  dataStream = response.GetResponseStream();
  Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
  reader = new StreamReader(dataStream, encode);

  responseText = reader.ReadToEnd();
  reader.Close();
  dataStream.Close();
  response.Close();
  }
  catch(Exception e)
  {
  Logger.writeln("从服务器获取数据失败");
  }
  return responseText;
  }
 


  //方法二
 

  using System;
using System.Xml;

namespace WriteXML
{
class Class1
{
static void Main( string[] args )
{
// 创建XmlTextWriter类的实例对象
XmlTextWriter textWriter = new XmlTextWriter("C:myXmFile.xml", null);

// 开始写过程,调用WriteStartDocument方法
textWriter.WriteStartDocument();

// 写入说明
textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
textWriter.WriteComment("myXmlFile.xml in root dir");

// 写入一个元素
textWriter.WriteStartElement("Name", "");
textWriter.WriteString("Student");
textWriter.WriteEndElement();

// 再写入一个元素
textWriter.WriteStartElement("Address", "");
textWriter.WriteString("Colony");
textWriter.WriteEndElement();

// 写入字符
char [] ch = new char[3];
ch[0] = 'a';
ch[1] = 'r';
ch[2] = 'c';
textWriter.WriteStartElement("Char");
textWriter.WriteChars(ch, 0, ch.Length);
textWriter.WriteEndElement();

// 写文档结束,调用WriteEndDocument方法
textWriter.WriteEndDocument();

// 关闭textWriter
textWriter.Close();
}
}
}

  %>

热门栏目