对XML文件操作

XML具体操作实现(增加,删除,更新,导出)
XML格式:
<?xml version="1.0" encoding="GB2312"?>
<app:Users xmlns:app="uri:appraisal">
  
<User id="000">
    
<Name>Jeffrey</Name>
    
<Sex></Sex>
    
<UserName>Jeffrey</UserName>
    
<UserID>1</UserID>
    
<Department>Jeffrey</Department>
    
<Working></Working>
    
<Power>0</Power>
    
<Position>1</Position>
    
<Post>
    
</Post>
    
<Tel>
    
</Tel>
  
</User>
  
<User id="001">
    
<Name>Tom</Name>
    
<Sex></Sex>
    
<UserName>tom</UserName>
    
<UserID>111</UserID>
    
<Department>host</Department>
    
<Working></Working>
    
<Power>2</Power>
    
<Position>工程师</Position>
    
<Post>
    
</Post>
    
<Tel>111</Tel>
  
</User>
  
<User id="002">
    
<Name>Jef</Name>
    
<Sex></Sex>
    
<UserName>jef</UserName>
    
<UserID>1</UserID>
    
<Department>jef </Department>
    
<Working></Working>
    
<Power>2</Power>
    
<Position>工程师</Position>
    
<Post>
    
</Post>
    
<Tel>121</Tel>
  
</User>
  
<User id="1">
    
<Name>1</Name>
    
<Sex>1</Sex>
    
<UserName>1</UserName>
    
<UseID>1</UseID>
    
<Department>1</Department>
    
<Working>1</Working>
    
<Power>1</Power>
    
<Position>1</Position>
    
<Post>1</Post>
    
<Tel>1</Tel>
  
</User>
</app:Users>

窗体加载的时候需要XML里面的数据
private void PopList(ListBox lbUsers)
        
{
            lbUsers.Items.Clear();
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            
//查找每个人员的根接点
            XmlNodeList xList=xdoc.SelectNodes("//User");


            
foreach(XmlNode xNode in xList)
            
{
                lbUsers.Items.Add(xNode.ChildNodes.Item(
0).InnerText);
            }

        }

ListBox里面选择转移的时候
private void lbUsers_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
string itemtext=lbUsers.GetItemText(lbUsers.SelectedItem);
            
string name=itemtext.Trim().ToString();
            DisplayNotes(name);
        }



/*实现当选择人改变的时候 把人名传入方法*/
    
private void DisplayNotes(string name)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            XmlNodeList NList
=xdoc.SelectNodes("//User[Name='"+name+"']");
            TextClear();
            
foreach(XmlNode node in NList)
            
{
                XmlNodeReader NReader
=new XmlNodeReader(node);
                
try
                
{
                    
while(NReader.Read())
                    
{
                        
if(NReader.NodeType==XmlNodeType.Element)
                        
{
                            
if(NReader.HasAttributes)
                            
{
                                
while(NReader.MoveToNextAttribute())
                                
{
                                    
if(NReader.Value!="uri:appraisal")
                                    
{
                                        txtID.Text
=NReader.Value;
                                    }

                                }

                            }

                        }

                        
if(NReader.Name=="Name")
                        
{
                            txtName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Sex")
                        
{
                            txtSex.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserName")
                        
{
                            txtUserName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserID")
                        
{
                            txtUserID.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Department")
                        
{
                            txtDepartment.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Working")
                        
{
                            txtWorking.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Power")
                        
{
                            txtPower.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Position")
                        
{
                            txtPosition.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Post")
                        
{
                            txtPost.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Tel")
                        
{
                            txtTel.Text
=NReader.ReadString();

                        }

                    }

                }

                
catch(Exception err)
                
{
                    
string errmsg="读取文件错误"+err.ToString();
                    MessageBox.Show(errmsg);
                }

                
finally
                
{
                    
if(NReader!=null)
                    
{
                        NReader.Close();
                    }

                }

            }

        
        }


新增用户到XML
private void btnAdd_Click(object sender, System.EventArgs e)
        
{
            
const string strFileName="User.xml";
            XmlTextWriter TWriter
=new XmlTextWriter(strFileName,Encoding.Default);
            TWriter.WriteStartDocument();
            TWriter.WriteStartElement(
"app","Users","uri:appraisal");
            TWriter.WriteStartElement(
"User");
            TWriter.WriteAttributeString(
"id",txtID.Text);
            TWriter.WriteElementString(
"Name",txtName.Text);
            TWriter.WriteElementString(
"Sex",txtSex.Text);
            TWriter.WriteElementString(
"UserName",txtUserName.Text);
            TWriter.WriteElementString(
"UseID",txtUserID.Text);
            TWriter.WriteElementString(
"Department",txtDepartment.Text);
            TWriter.WriteElementString(
"Working",txtWorking.Text);
            TWriter.WriteElementString(
"Power",txtPower.Text);
            TWriter.WriteElementString(
"Position",txtPosition.Text);
            TWriter.WriteElementString(
"Post",txtPost.Text);
            TWriter.WriteElementString(
"Tel",txtTel.Text);

            TWriter.WriteElementString(
"Tel1", txtTel.Text);

            TWriter.WriteEndElement();
            TWriter.WriteEndElement();
            TWriter.WriteEndDocument();

            TWriter.Flush();
            TWriter.Close();

            ImportUser(
"User.xml");
        }



private void ImportUser(string path)
        
{    
            
string FilePath="Users.xml";

            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(FilePath);

            XmlDocument xdocImp
=new XmlDocument();
            xdocImp.Load(path);

            XmlNodeList pNodes
=xdocImp.SelectNodes("//User");

            NameTable nametable
=new NameTable();
            XmlNamespaceManager nameMan
=new XmlNamespaceManager(nametable);
            nameMan.AddNamespace(
"app","uri:appraisal");

            
foreach(XmlNode pNode in pNodes)
            
{
                XmlNode pNodeImp
=xdoc.SelectSingleNode("//User[Name='"+pNode.ChildNodes.Item(0).InnerXml+"']");
                
if(pNodeImp==null)  
                
{
                    XmlNode importedNode
=xdoc.ImportNode(pNode,true);
                    xdoc.SelectSingleNode(
"//app:Users",nameMan).AppendChild(importedNode);
                }

            }

            xdoc.Save(FilePath);
            PopList(lbUsers);
        }


删除用户
private void btnDelete_Click(object sender, System.EventArgs e)
        
{
            
string itemtext=lbUsers.GetItemText(lbUsers.SelectedItem);
            
string name=itemtext.Trim().ToString();
            TextClear();
            DeleteNotes(name);
        }


        
private void DeleteNotes(string name)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");

            NameTable nametable
=new NameTable();
            XmlNamespaceManager nameMan
=new XmlNamespaceManager(nametable);
            nameMan.AddNamespace(
"app","uri:appraisal");

            XmlNode rootnode
=xdoc.SelectSingleNode("//app:Users",nameMan);
            XmlNode node
=xdoc.SelectSingleNode("//User[Name='"+name+"']");
            rootnode.RemoveChild(node);
            xdoc.Save(
"Users.xml");
            PopList(lbUsers);
        }
更新用户
private void btnUpdate_Click(object sender, System.EventArgs e)
        
{
            
string itemtext=lbUsers.GetItemText(lbUsers.SelectedItem);
            
string name=itemtext.Trim().ToString();
            UpdateNotes(name);
        }


        
private void UpdateNotes(string name)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            
            
//创建一个文档片断来存储要替换的节点
            XmlDocumentFragment xdocFrag=xdoc.CreateDocumentFragment();
            
//新节点的根节点
            XmlElement newElement=xdoc.CreateElement("User");
            XmlAttribute newID
=xdoc.CreateAttribute("id");
            newID.Value
=txtID.Text;
            newElement.Attributes.SetNamedItem(newID);

            XmlElement newName
=xdoc.CreateElement("Name");
            newName.InnerText
=txtName.Text;
            newElement.AppendChild(newName);

            XmlElement newSex
=xdoc.CreateElement("Sex");
            newSex.InnerText
=txtSex.Text;
            newElement.AppendChild(newSex);

            XmlElement newUserName
=xdoc.CreateElement("UserName");
            newUserName.InnerText
=txtUserName.Text;
            newElement.AppendChild(newUserName);

            XmlElement newUserID
=xdoc.CreateElement("UserID");
            newUserID.InnerText
=txtUserID.Text;
            newElement.AppendChild(newUserID);

            XmlElement newWorking
=xdoc.CreateElement("Working");
            newWorking.InnerText
=txtWorking.Text;
            newElement.AppendChild(newWorking);

            XmlElement newDepartment
=xdoc.CreateElement("Department");
            newDepartment.InnerText
=txtDepartment.Text;
            newElement.AppendChild(newDepartment);

            XmlElement newPosition
=xdoc.CreateElement("Position");
            newPosition.InnerText
=txtPosition.Text;
            newElement.AppendChild(newPosition);

            XmlElement newPost
=xdoc.CreateElement("Post");
            newPost.InnerText
=txtPost.Text;
            newElement.AppendChild(newPost);

            XmlElement newPower
=xdoc.CreateElement("Power");
            newPower.InnerText
=txtPower.Text;
            newElement.AppendChild(newPower);

            XmlElement newTel
=xdoc.CreateElement("Tel");
            newTel.InnerText
=txtTel.Text;
            newTel.AppendChild(newTel);



            
//XmlElement newTel1 = xdoc.CreateElement("Tel1");
            
//newTel1.InnerText = txtTel.Text;
            
//newTel1.AppendChild(newTel);
            


            
//把新创建的节点加入文档片断
            xdocFrag.AppendChild(newElement);
            
            
//确定要替换的节点作替换
            NameTable nametable=new NameTable();
            XmlNamespaceManager nameMan
=new XmlNamespaceManager(nametable);
            nameMan.AddNamespace(
"app","uri:appraisal");

            XmlElement xUsers
=(XmlElement)xdoc.SelectSingleNode("//app:Users",nameMan);
            XmlElement xChild
=(XmlElement)xUsers.ChildNodes[1];
            xUsers.ReplaceChild(xdocFrag.FirstChild,xChild);

            xdoc.Save(
"Users.xml");

            PopList(lbUsers);

        
        }
导出用户到XML
private void btnExport_Click(object sender, System.EventArgs e)
        
{
            
if(txtExport.Text.Trim().Length>0)
                ExportUsers(txtExport.Text);
            MessageBox.Show(
"导出成功!");
        }


        
private void ExportUsers(string path)
        
{
            
const string OrigPath="Users.xml";
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(OrigPath);
            xdoc.Save(path);
        
        }
按照用户名进行查询
private void btnSearch_Click(object sender, System.EventArgs e)
        
{
            FindContact(txtSearch.Text);
        }

/*符合名称的人 进行查询*/
        
private void FindContact(string query)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            XmlNodeList xList
=xdoc.SelectNodes("//User[Name='"+query+"']");
            
foreach(XmlNode node in xList)
            
{
                XmlNodeReader NReader
=new XmlNodeReader(node);
                
try
                
{
                    
while(NReader.Read())
                    
{
                        
if(NReader.NodeType==XmlNodeType.Element)
                        
{
                            
if(NReader.HasAttributes)
                            
{
                                
while(NReader.MoveToNextAttribute())
                                
{
                                    
if(NReader.Value!="uri:appraisal")
                                    
{
                                        txtID.Text
=NReader.Value;
                                    }

                                }

                            }

                        }

                        
if(NReader.Name=="Name")
                        
{
                            txtName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Sex")
                        
{
                            txtSex.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserName")
                        
{
                            txtUserName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserID")
                        
{
                            txtUserID.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Department")
                        
{
                            txtDepartment.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Working")
                        
{
                            txtWorking.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Power")
                        
{
                            txtPower.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Position")
                        
{
                            txtPosition.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Post")
                        
{
                            txtPost.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Tel")
                        
{
                            txtTel.Text
=NReader.ReadString();
                            
//
                        }

                    }

                }

                
catch(Exception err)
                
{
                    
string errmsg="读取文件错误"+err.ToString();
                    MessageBox.Show(errmsg);
                }

                
finally
                
{
                    
if(NReader!=null)
                    
{
                        NReader.Close();
                    }

                }

            }

        }


全部代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text;

namespace UserManagement
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class frmUser : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.ListBox lbUsers;
        
private System.Windows.Forms.TextBox txtSex;
        
private System.Windows.Forms.TextBox txtUserID;
        
private System.Windows.Forms.TextBox txtUserName;
        
private System.Windows.Forms.TextBox txtID;
        
private System.Windows.Forms.TextBox txtName;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.Label lbID;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.Label label4;
        
private System.Windows.Forms.Label label5;
        
private System.Windows.Forms.Label label6;
        
private System.Windows.Forms.Label label7;
        
private System.Windows.Forms.Label label8;
        
private System.Windows.Forms.Label lbUserName;
        
private System.Windows.Forms.TextBox txtPower;
        
private System.Windows.Forms.TextBox txtPosition;
        
private System.Windows.Forms.TextBox txtTel;
        
private System.Windows.Forms.TextBox txtWorking;
        
private System.Windows.Forms.TextBox txtPost;
        
private System.Windows.Forms.TextBox txtDepartment;
        
private System.Windows.Forms.Label label9;
        
private System.Windows.Forms.Label label10;
        
private System.Windows.Forms.Button btnExport;
        
private System.Windows.Forms.TextBox txtExport;
        
private System.Windows.Forms.Button btnAdd;
        
private System.Windows.Forms.Button btnUpdate;
        
private System.Windows.Forms.Button btnDelete;
        
private System.Windows.Forms.Button btnSearch;
        
private System.Windows.Forms.TextBox txtSearch;
        
private System.Windows.Forms.Button btnClear;
        
private System.Windows.Forms.MainMenu mnuMain;
        
private System.Windows.Forms.MenuItem menuItem1;
        
private System.Windows.Forms.MenuItem menuExit;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public frmUser()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new frmUser());
        }


        
private void frmUser_Load(object sender, System.EventArgs e)
        
{
            PopList(lbUsers);
        }


        
private void PopList(ListBox lbUsers)
        
{
            lbUsers.Items.Clear();
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            
//查找每个人员的根接点
            XmlNodeList xList=xdoc.SelectNodes("//User");


            
foreach(XmlNode xNode in xList)
            
{
                lbUsers.Items.Add(xNode.ChildNodes.Item(
0).InnerText);
            }

        }


        
private void lbUsers_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
string itemtext=lbUsers.GetItemText(lbUsers.SelectedItem);
            
string name=itemtext.Trim().ToString();
            DisplayNotes(name);
        }


        
private void TextClear()
        
{
            txtName.Text
="";
            txtID.Text
="";
            txtSex.Text
="";
            txtUserName.Text
="";
            txtUserID.Text
="";
            txtWorking.Text
="";
            txtDepartment.Text
="";
            txtPosition.Text
="";
            txtPost.Text
="";
            txtPower.Text
="";
            txtTel.Text
="";
        }


        
private void DisplayNotes(string name)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            XmlNodeList NList
=xdoc.SelectNodes("//User[Name='"+name+"']");
            TextClear();
            
foreach(XmlNode node in NList)
            
{
                XmlNodeReader NReader
=new XmlNodeReader(node);
                
try
                
{
                    
while(NReader.Read())
                    
{
                        
if(NReader.NodeType==XmlNodeType.Element)
                        
{
                            
if(NReader.HasAttributes)
                            
{
                                
while(NReader.MoveToNextAttribute())
                                
{
                                    
if(NReader.Value!="uri:appraisal")
                                    
{
                                        txtID.Text
=NReader.Value;
                                    }

                                }

                            }

                        }

                        
if(NReader.Name=="Name")
                        
{
                            txtName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Sex")
                        
{
                            txtSex.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserName")
                        
{
                            txtUserName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserID")
                        
{
                            txtUserID.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Department")
                        
{
                            txtDepartment.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Working")
                        
{
                            txtWorking.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Power")
                        
{
                            txtPower.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Position")
                        
{
                            txtPosition.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Post")
                        
{
                            txtPost.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Tel")
                        
{
                            txtTel.Text
=NReader.ReadString();

                        }

                    }

                }

                
catch(Exception err)
                
{
                    
string errmsg="读取文件错误"+err.ToString();
                    MessageBox.Show(errmsg);
                }

                
finally
                
{
                    
if(NReader!=null)
                    
{
                        NReader.Close();
                    }

                }

            }

        
        }


        
private void btnExport_Click(object sender, System.EventArgs e)
        
{
            
if(txtExport.Text.Trim().Length>0)
                ExportUsers(txtExport.Text);
            MessageBox.Show(
"导出成功!");
        }


        
private void ExportUsers(string path)
        
{
            
const string OrigPath="Users.xml";
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(OrigPath);
            xdoc.Save(path);
        
        }


        
private void btnAdd_Click(object sender, System.EventArgs e)
        
{
            
const string strFileName="User.xml";
            XmlTextWriter TWriter
=new XmlTextWriter(strFileName,Encoding.Default);
            TWriter.WriteStartDocument();
            TWriter.WriteStartElement(
"app","Users","uri:appraisal");
            TWriter.WriteStartElement(
"User");
            TWriter.WriteAttributeString(
"id",txtID.Text);
            TWriter.WriteElementString(
"Name",txtName.Text);
            TWriter.WriteElementString(
"Sex",txtSex.Text);
            TWriter.WriteElementString(
"UserName",txtUserName.Text);
            TWriter.WriteElementString(
"UseID",txtUserID.Text);
            TWriter.WriteElementString(
"Department",txtDepartment.Text);
            TWriter.WriteElementString(
"Working",txtWorking.Text);
            TWriter.WriteElementString(
"Power",txtPower.Text);
            TWriter.WriteElementString(
"Position",txtPosition.Text);
            TWriter.WriteElementString(
"Post",txtPost.Text);
            TWriter.WriteElementString(
"Tel",txtTel.Text);

            TWriter.WriteElementString(
"Tel1", txtTel.Text);

            TWriter.WriteEndElement();
            TWriter.WriteEndElement();
            TWriter.WriteEndDocument();

            TWriter.Flush();
            TWriter.Close();

            ImportUser(
"User.xml");
        }

        
        
private void ImportUser(string path)
        
{    
            
string FilePath="Users.xml";

            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(FilePath);

            XmlDocument xdocImp
=new XmlDocument();
            xdocImp.Load(path);

            XmlNodeList pNodes
=xdocImp.SelectNodes("//User");

            NameTable nametable
=new NameTable();
            XmlNamespaceManager nameMan
=new XmlNamespaceManager(nametable);
            nameMan.AddNamespace(
"app","uri:appraisal");

            
foreach(XmlNode pNode in pNodes)
            
{
                XmlNode pNodeImp
=xdoc.SelectSingleNode("//User[Name='"+pNode.ChildNodes.Item(0).InnerXml+"']");
                
if(pNodeImp==null)  
                
{
                    XmlNode importedNode
=xdoc.ImportNode(pNode,true);
                    xdoc.SelectSingleNode(
"//app:Users",nameMan).AppendChild(importedNode);
                }

            }

            xdoc.Save(FilePath);
            PopList(lbUsers);
        }


        
private void btnUpdate_Click(object sender, System.EventArgs e)
        
{
            
string itemtext=lbUsers.GetItemText(lbUsers.SelectedItem);
            
string name=itemtext.Trim().ToString();
            UpdateNotes(name);
        }


        
private void UpdateNotes(string name)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            
            
//创建一个文档片断来存储要替换的节点
            XmlDocumentFragment xdocFrag=xdoc.CreateDocumentFragment();
            
//新节点的根节点
            XmlElement newElement=xdoc.CreateElement("User");
            XmlAttribute newID
=xdoc.CreateAttribute("id");
            newID.Value
=txtID.Text;
            newElement.Attributes.SetNamedItem(newID);

            XmlElement newName
=xdoc.CreateElement("Name");
            newName.InnerText
=txtName.Text;
            newElement.AppendChild(newName);

            XmlElement newSex
=xdoc.CreateElement("Sex");
            newSex.InnerText
=txtSex.Text;
            newElement.AppendChild(newSex);

            XmlElement newUserName
=xdoc.CreateElement("UserName");
            newUserName.InnerText
=txtUserName.Text;
            newElement.AppendChild(newUserName);

            XmlElement newUserID
=xdoc.CreateElement("UserID");
            newUserID.InnerText
=txtUserID.Text;
            newElement.AppendChild(newUserID);

            XmlElement newWorking
=xdoc.CreateElement("Working");
            newWorking.InnerText
=txtWorking.Text;
            newElement.AppendChild(newWorking);

            XmlElement newDepartment
=xdoc.CreateElement("Department");
            newDepartment.InnerText
=txtDepartment.Text;
            newElement.AppendChild(newDepartment);

            XmlElement newPosition
=xdoc.CreateElement("Position");
            newPosition.InnerText
=txtPosition.Text;
            newElement.AppendChild(newPosition);

            XmlElement newPost
=xdoc.CreateElement("Post");
            newPost.InnerText
=txtPost.Text;
            newElement.AppendChild(newPost);

            XmlElement newPower
=xdoc.CreateElement("Power");
            newPower.InnerText
=txtPower.Text;
            newElement.AppendChild(newPower);

            XmlElement newTel
=xdoc.CreateElement("Tel");
            newTel.InnerText
=txtTel.Text;
            newTel.AppendChild(newTel);



            
//XmlElement newTel1 = xdoc.CreateElement("Tel1");
            
//newTel1.InnerText = txtTel.Text;
            
//newTel1.AppendChild(newTel);
            


            
//把新创建的节点加入文档片断
            xdocFrag.AppendChild(newElement);
            
            
//确定要替换的节点作替换
            NameTable nametable=new NameTable();
            XmlNamespaceManager nameMan
=new XmlNamespaceManager(nametable);
            nameMan.AddNamespace(
"app","uri:appraisal");

            XmlElement xUsers
=(XmlElement)xdoc.SelectSingleNode("//app:Users",nameMan);
            XmlElement xChild
=(XmlElement)xUsers.ChildNodes[1];
            xUsers.ReplaceChild(xdocFrag.FirstChild,xChild);

            xdoc.Save(
"Users.xml");

            PopList(lbUsers);

        
        }


        
private void btnDelete_Click(object sender, System.EventArgs e)
        
{
            
string itemtext=lbUsers.GetItemText(lbUsers.SelectedItem);
            
string name=itemtext.Trim().ToString();
            TextClear();
            DeleteNotes(name);
        }


        
private void DeleteNotes(string name)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");

            NameTable nametable
=new NameTable();
            XmlNamespaceManager nameMan
=new XmlNamespaceManager(nametable);
            nameMan.AddNamespace(
"app","uri:appraisal");

            XmlNode rootnode
=xdoc.SelectSingleNode("//app:Users",nameMan);
            XmlNode node
=xdoc.SelectSingleNode("//User[Name='"+name+"']");
            rootnode.RemoveChild(node);
            xdoc.Save(
"Users.xml");
            PopList(lbUsers);
        }


        
private void btnSearch_Click(object sender, System.EventArgs e)
        
{
            FindContact(txtSearch.Text);
        }


        
private void FindContact(string query)
        
{
            XmlDocument xdoc
=new XmlDocument();
            xdoc.Load(
"Users.xml");
            XmlNodeList xList
=xdoc.SelectNodes("//User[Name='"+query+"']");
            
foreach(XmlNode node in xList)
            
{
                XmlNodeReader NReader
=new XmlNodeReader(node);
                
try
                
{
                    
while(NReader.Read())
                    
{
                        
if(NReader.NodeType==XmlNodeType.Element)
                        
{
                            
if(NReader.HasAttributes)
                            
{
                                
while(NReader.MoveToNextAttribute())
                                
{
                                    
if(NReader.Value!="uri:appraisal")
                                    
{
                                        txtID.Text
=NReader.Value;
                                    }

                                }

                            }

                        }

                        
if(NReader.Name=="Name")
                        
{
                            txtName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Sex")
                        
{
                            txtSex.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserName")
                        
{
                            txtUserName.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="UserID")
                        
{
                            txtUserID.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Department")
                        
{
                            txtDepartment.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Working")
                        
{
                            txtWorking.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Power")
                        
{
                            txtPower.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Position")
                        
{
                            txtPosition.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Post")
                        
{
                            txtPost.Text
=NReader.ReadString();
                        }

                        
else if(NReader.Name=="Tel")
                        
{
                            txtTel.Text
=NReader.ReadString();
                            
//
                        }

                    }

                }

                
catch(Exception err)
                
{
                    
string errmsg="读取文件错误"+err.ToString();
                    MessageBox.Show(errmsg);
                }

                
finally
                
{
                    
if(NReader!=null)
                    
{
                        NReader.Close();
                    }

                }

            }

        }


        
private void btnClear_Click(object sender, System.EventArgs e)
        
{
            TextClear();
        }


        
private void menuExit_Click(object sender, System.EventArgs e)
        
{
            
this.Close();
        }


    
    }

}

posted @ 2007-03-28 17:59  jhtchina  阅读(7408)  评论(1编辑  收藏  举报