如何把XML文档显示在text文本框中? 如题。找了找,好像应该用XmlTextWriter,但是XmlTextWriter只能输出到Console.out或者文件中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 读到流里面,然后:System.IO.StreamReader _read=new System.IO.StreamReader(sdata.Request.InputStream);String str=_read.readtoend(); 带格式的显示到文本框中可以用如下的方法:XmlDocument doc = new XmlDocument();doc.Load("xmlfile.xml");StringWriter writer1 = new StringWriter();XmlTextWriter writer2 = new XmlTextWriter(writer1);writer2.Formatting = Formatting.Indented;writer2.Indentation=1;writer2.IndentChar= '\t';doc.WriteTo(writer2);this.textBox1.Text = writer1.ToString(); 我有一个例子就是把网站的常量CONST,放在XML文件当中,网站启动时加载。在后台可以维护重新写入XML文件当中。其中CONST.CS文件你看看,这个主要是载入。+++++++++++++++++++++++++++++++++++++++ public class Const { public static string sitename=""; public static string domain=""; public static string webemail=""; public static string weblogo=""; public static string regtime=""; public static string keywords=""; public static string description=""; public static string copyright=""; public static string maxonline=""; //历史最高在线人数 public static string onlinetime=""; //历史最高在线发生时间 public static string isregister=""; //是否允许注册 public static string minusername=""; //最短用户名长度 public static string maxusername=""; // 最长用户名长度 static Const() { // // TODO: 在此处添加构造函数逻辑 XmlDocument xmldoc=new XmlDocument(); xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("/hbtd/SiteConst.xml")); //装入XML文档 XmlNodeReader reader=new XmlNodeReader(xmldoc); //读取 XmlNode node=xmldoc.DocumentElement; if(reader.Read()) { sitename=node["sitename"].InnerText; domain=node["domain"].InnerText; webemail=node["webemail"].InnerText; regtime=node["regtime"].InnerText; weblogo=node["weblogo"].InnerText; keywords=node["keywords"].InnerText; description=node["description"].InnerText; copyright=node["copyright"].InnerText; } reader.Close(); } }++++++++++++++++++++++++++++++++++++++下面是后台维护,并包含了如何显示在TEXTBOX中的代码和重写XML文件的代码:++++++++++++++++++++++++++++++++++++private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 this.t_sitename.Value=Const.sitename; this.t_weblogo.Value=Const.weblogo; this.t_webemail.Value=Const.webemail; this.t_keywords.Value=Const.keywords; this.t_domain.Value=Const.domain; this.t_description.Value=Const.description; this.t_copyright.Value=Const.copyright; this.t_maxonline.Value=Const.maxonline; this.t_maxusername.Value=Const.maxusername; this.t_minusername.Value=Const.minusername; this.t_onlinetime.Value=Const.onlinetime; if(Const.isregister=="1") { this.Radio2.Checked=true; } else { this.Radio1.Checked=true; } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.btn_update.Click += new System.EventHandler(this.btn_update_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void btn_update_Click(object sender, System.EventArgs e) { XmlDocument xmldoc=new XmlDocument(); xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("../SiteConst.xml")); //装入XML文档 XmlNodeReader reader=new XmlNodeReader(xmldoc); //读取 XmlNode node=xmldoc.DocumentElement; if(reader.Read()) { node["sitename"].InnerText=Request["t_sitename"]; node["domain"].InnerText=Request["t_domain"]; node["webemail"].InnerText=Request["t_webemail"]; node["weblogo"].InnerText=Request["t_weblogo"]; node["keywords"].InnerText=Request["t_keywords"]; node["description"].InnerText=Request["t_description"]; node["copyright"].InnerText=Request["t_copyright"]; node["maxonline"].InnerText=Request["t_maxonline"]; node["maxusername"].InnerText=Request["t_maxusername"]; node["minusername"].InnerText=Request["t_minusername"]; node["onlinetime"].InnerText=Request["t_onlinetime"]; if(this.Radio2.Checked==true) { node["isregister"].InnerText="1"; } else { node["isregister"].InnerText="0"; } } xmldoc.Save(System.Web.HttpContext.Current.Server.MapPath("../SiteConst.xml")); reader.Close(); Response.Write("<script>alert('修改成功!');window.location.href='main.aspx';</script>"); } }我想对你会有帮助!!! 专家在哪???cs项目中怎么让文本框不能输入负号,汉字,英文!!!! DropDownList选择项 基础问题 winform 问题 高手请进,我调用控制台程序的时候如何加入参数 在用代码获取网页输出的时候WebResponse出现如下提示 新手问题。关于枚举,和函数调用的问题。 关于access 导出为excel问题 找不到WCF服务 c#里怎么使用VBA?急呀,这是我的代码,知道的兄弟给指点一下! 在asp.net中怎么获得域登录名? C#中C/S开发如何做到瘦客户端 帮忙看两段代码,看怎样可以建立起通讯关系。我在调试是总是出错!
System.IO.StreamReader _read=new System.IO.StreamReader(sdata.Request.InputStream);
String str=_read.readtoend();
XmlDocument doc = new XmlDocument();
doc.Load("xmlfile.xml");
StringWriter writer1 = new StringWriter();
XmlTextWriter writer2 = new XmlTextWriter(writer1);
writer2.Formatting = Formatting.Indented;
writer2.Indentation=1;
writer2.IndentChar= '\t';
doc.WriteTo(writer2);
this.textBox1.Text = writer1.ToString();
其中CONST.CS文件你看看,这个主要是载入。
+++++++++++++++++++++++++++++++++++++++
public class Const
{
public static string sitename="";
public static string domain="";
public static string webemail="";
public static string weblogo="";
public static string regtime="";
public static string keywords="";
public static string description="";
public static string copyright="";
public static string maxonline=""; //历史最高在线人数
public static string onlinetime=""; //历史最高在线发生时间
public static string isregister=""; //是否允许注册
public static string minusername=""; //最短用户名长度
public static string maxusername=""; // 最长用户名长度
static Const()
{
//
// TODO: 在此处添加构造函数逻辑
XmlDocument xmldoc=new XmlDocument();
xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("/hbtd/SiteConst.xml")); //装入XML文档
XmlNodeReader reader=new XmlNodeReader(xmldoc); //读取
XmlNode node=xmldoc.DocumentElement;
if(reader.Read())
{
sitename=node["sitename"].InnerText;
domain=node["domain"].InnerText;
webemail=node["webemail"].InnerText;
regtime=node["regtime"].InnerText;
weblogo=node["weblogo"].InnerText;
keywords=node["keywords"].InnerText;
description=node["description"].InnerText;
copyright=node["copyright"].InnerText;
}
reader.Close();
}
}
++++++++++++++++++++++++++++++++++++++下面是后台维护,并包含了如何显示在TEXTBOX中的代码和重写XML文件的代码:
++++++++++++++++++++++++++++++++++++private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.t_sitename.Value=Const.sitename;
this.t_weblogo.Value=Const.weblogo;
this.t_webemail.Value=Const.webemail;
this.t_keywords.Value=Const.keywords;
this.t_domain.Value=Const.domain;
this.t_description.Value=Const.description;
this.t_copyright.Value=Const.copyright;
this.t_maxonline.Value=Const.maxonline;
this.t_maxusername.Value=Const.maxusername;
this.t_minusername.Value=Const.minusername;
this.t_onlinetime.Value=Const.onlinetime;
if(Const.isregister=="1")
{
this.Radio2.Checked=true; }
else
{
this.Radio1.Checked=true;
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_update.Click += new System.EventHandler(this.btn_update_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btn_update_Click(object sender, System.EventArgs e)
{
XmlDocument xmldoc=new XmlDocument();
xmldoc.Load(System.Web.HttpContext.Current.Server.MapPath("../SiteConst.xml")); //装入XML文档
XmlNodeReader reader=new XmlNodeReader(xmldoc); //读取
XmlNode node=xmldoc.DocumentElement;
if(reader.Read())
{
node["sitename"].InnerText=Request["t_sitename"];
node["domain"].InnerText=Request["t_domain"];
node["webemail"].InnerText=Request["t_webemail"];
node["weblogo"].InnerText=Request["t_weblogo"];
node["keywords"].InnerText=Request["t_keywords"];
node["description"].InnerText=Request["t_description"];
node["copyright"].InnerText=Request["t_copyright"];
node["maxonline"].InnerText=Request["t_maxonline"];
node["maxusername"].InnerText=Request["t_maxusername"];
node["minusername"].InnerText=Request["t_minusername"];
node["onlinetime"].InnerText=Request["t_onlinetime"];
if(this.Radio2.Checked==true)
{
node["isregister"].InnerText="1";
}
else
{
node["isregister"].InnerText="0";
}
}
xmldoc.Save(System.Web.HttpContext.Current.Server.MapPath("../SiteConst.xml"));
reader.Close();
Response.Write("<script>alert('修改成功!');window.location.href='main.aspx';</script>");
}
}我想对你会有帮助!!!