关于C# 读取XML 的问题~救急!!
本菜鸟刚刚学C#,求大虾们解救啊,谢谢!!~~
现在想读取XML上的内容,并在文本框,textbox1,textbox2,textbox3 上分别显示标签<UserCode> <UserName> <Userpwd>
的内容 ,但运行的时候,文本框上只能显示最后一个<User>标签的内容,以下是代码:
class XML
{
public string re;
public string UserCode;
public string UserName;
public string UserPassword;
public void WriteXML(string usercode, string username, string userpassword)
{
string FileName = @"C:\Users\leo\Desktop\test\test\save.xml";
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.Load(FileName);
//添加元素--UserCode
XmlElement ele = myDoc.CreateElement("UserCode");
XmlText text = myDoc.CreateTextNode(usercode);
//添加元素--UserName
XmlElement ele1 = myDoc.CreateElement("UserName");
XmlText text1 = myDoc.CreateTextNode(username);
//添加元素--UserPwd
XmlElement ele2 = myDoc.CreateElement("UserPwd");
XmlText text2 = myDoc.CreateTextNode(userpassword);
//添加节点 User要对应我们xml文件中的节点名字
XmlNode newElem = myDoc.CreateNode("element", "User", "");
//在节点中添加元素
newElem.AppendChild(ele);
newElem.LastChild.AppendChild(text);
newElem.AppendChild(ele1);
newElem.LastChild.AppendChild(text1);
newElem.AppendChild(ele2);
newElem.LastChild.AppendChild(text2);
//将节点添加到文档中
XmlElement root = myDoc.DocumentElement;
root.AppendChild(newElem);
//保存
myDoc.Save(FileName);
}
public void ReadXML()
{
try
{
using (XmlReader reader = XmlReader.Create(@"C:\Users\leo\Desktop\test\test\save.xml"))
{
reader.Read();
reader.ReadStartElement("UserLogin");
reader.ReadStartElement("User");
reader.ReadStartElement("UserCode");
UserCode = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("UserName");
UserName = reader.ReadString();
reader.ReadEndElement();
reader.ReadStartElement("UserPwd");
UserPassword = reader.ReadString();
reader.ReadEndElement();
reader.ReadEndElement();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
re = reader.Name;
if (re == "UserLogin")
{
}
else if (re == "UserCode")
{
if (reader.Read())