现在的位置: 首页 > 综合 > 正文

PDA中XML的读取和写入

2013年11月18日 ⁄ 综合 ⁄ 共 1569字 ⁄ 字号 评论关闭

 public class XMLOperation

{

        XmlDocument xmldoc = new XmlDocument();
        XmlNode xnod;
        XmlElement xenl;

       //写入XML

       private void CreateXmlDocument()
        {
            //描述信息
            xnod = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xnod);

            //根元素
            xenl = xmldoc.CreateElement("", "body", "");
            xmldoc.AppendChild(xenl);

            XmlNode nod = xmldoc.SelectSingleNode("body");

            //设置元素的值
            XmlElement element;
            element = xmldoc.CreateElement("item");
            element.SetAttribute("Name", this.txtUserName.Text.ToString());
            element.SetAttribute("PWD", this.txtUserPwd.Text.Trim());
            nod.AppendChild(element);

           //注意:得到当前应用程序所在位置
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            xmldoc.Save(path + "//XmlTest.xml");
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + @"/XmlTest.xml");
            //验证XML信息,查看是否存在用户信息
            XmlNodeList nodeList = xmlDoc.SelectNodes("body/item");

            for (int row = 0; row < nodeList.Count; row++)
            {
                MessageBox.Show(nodeList[row].Attributes["Name"].Value.ToString());
                MessageBox.Show(nodeList[row].Attributes["PWD"].Value.ToString());
            }        }

}

抱歉!评论已关闭.