我创建了一个窗体 有两个对话框和一个确定按钮 现在要求在两个对话框中输入字符串 然后按确定键 就把这两个字符串存到一个Xml文挡中。本人不知道什么是xml文挡 应该这样创建 我怎么才能通过按确定键创建一个XML文挡 把字符串存进去呀。如果帮我搞定 我再加100分
解决方案 »
- 设置nlb集群,怎么实现remoting的负载均衡
- 如何简单理解同步和异步?
- 关于C#加载组件的问题?
- 如果将outlook2007窗体区域中的listview控件映射到c#outlook外接程序中,以便调用.
- 很不能理解为什么XmlElement不让实例化
- 水晶报表~ 用InstallShield 打包“Cannot find Keycode, or invalid keycode”的错误提示?
- 纳闷,《C#入门经典》 第52页 关于 if 语句的一个问题。
- WPF窗体继承问题
- 大家的visual.net版本号是多少呀?
- 哪里能下载到C#的电子书?
- 如何获取本机机器码,地区语言
- 如何使datagrid的一列顺序显示数字
{
XmlTextWriter wr=new XmlTextWriter(Server.MapPath("hehe.xml"),null);
wr.WriteStartDocument();
wr.WriteStartElement("persons");
wr.WriteStartElement("person"); wr.WriteStartElement("username");
wr.WriteString(this.TextBox1.Text);
wr.WriteEndElement(); wr.WriteStartElement("password");
wr.WriteString(this.TextBox2.Text);
wr.WriteEndElement(); wr.WriteEndElement();
wr.WriteEndElement();
wr.WriteEndDocument();
wr.Close();
}
在TextBox1输入testxml,TextBox2输入test
然后生成 hehe.xml,内容如下:
<?xml version="1.0">
<persons>
<person>
<username>testxml</username>
<password>test</password>
</person>
</persons>
/// 从xml文件中读取指定键的值
/// </summary>
/// <param name="strKey">键</param>
/// <param name="strValue">值</param>
/// <returns>返回指定键的值</returns>
public static string ReadValue(string strKey)
{
System.IO.FileInfo FileInfo = new System.IO.FileInfo(文件路径);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(FileInfo.FullName);
foreach(XmlNode node in xmlDocument["configuration"]["appSettings"])
{
if(node.Attributes["key"].Value == strKey)
{
return node.Attributes["value"].Value;
}
}
return "";
}
/// <summary>
/// 对xml文件的指定键写入指定值
/// </summary>
/// <param name="strKey">键</param>
/// <param name="strValue">值</param>
public static void WriterValue(string strKey,string strValue)
{
System.IO.FileInfo FileInfo= new System.IO.FileInfo(xml文件路径);
XmlDocument XmlDocument1 =new XmlDocument(); XmlDocument1.Load(FileInfo.FullName);
foreach(XmlNode Node in XmlDocument1["configuration"]["appSettings"])
{
if(Node.Attributes["key"].Value == strKey)
{
Node.Attributes["value"].Value = strValue;
}
}
XmlDocument1.Save(FileInfo.FullName);
}
XmlTextWriter wr=new XmlTextWriter(Server.MapPath("hehe.xml"),null);该为:
XmlTextWriter wr=new XmlTextWriter(Application.StartupPath+"\\..\\..\\hehe.xml",null);
就可以了
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;
namespace Zhzuo
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string strXml="<?xml version=\"1.0\"?>"
+"<Data>"
+"<Head>"
+"<Nodeid>1111</Nodeid>"
+"<Subid>2222</Subid>"
+"<Version>2004</Version>"
+"<Date>20040302</Date>"
+"<Time>101500</Time>"
+"</Head>"
+"<Body>"
+"<Code>01</Code>"
+"<Name>深圳</Name>"
+"<IdType>0</IdType>"
+"<Idno>110258740824082</Idno>"
+"</Body>"
+"</Data>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
string vv;
vv = doc.GetElementsByTagName("Version")[0].InnerText;
Console.WriteLine(vv);
vv = doc.SelectNodes("//Version")[0].InnerText;
Console.WriteLine(vv);
Console.ReadLine();
}
}
}