asp.net中对xml的读写(二)

来源:互联网 发布:王者荣耀辅助软件 编辑:程序博客网 时间:2024/05/21 19:31
 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;

/// <summary>
///xmlrw 的摘要说明
/// </summary>
public class xmlrw
{
 public xmlrw()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    /// <summary>
    /// 登录的checkbox1是否选中
    /// </summary>
    public static bool checkbox1;
    /// <summary>
    /// 登录的checkbox2是否选中
    /// </summary>
    public static bool checkbox2;
    /// <summary>
    /// 帐号
    /// </summary>
    public static string username;
    /// <summary>
    /// 密码
    /// </summary>
    public static string userpwd;
    public static string lujing = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/XMLFile.xml");
    public static  void xmlread()
    {

        XmlDocument xml = new XmlDocument();
       

        if (File.Exists(lujing))
        {
            xml.Load(lujing);
            XmlNodeList xnlist = xml.SelectSingleNode("body").ChildNodes;
            if (xnlist[0].InnerText != null && xnlist[0].InnerText != "")
            {
                checkbox1 = Convert.ToBoolean(xnlist[0].InnerText);
            }
            else
            {
                checkbox1 = false;
            }
            if (xnlist[1].InnerText != null && xnlist[1].InnerText != "")
            {
                checkbox2 = Convert.ToBoolean(xnlist[1].InnerText);
            }
            else
            {
                checkbox2 = false;
            }
            if (xnlist[2].InnerText != null && xnlist[2].InnerText != "")
            {
                username = xnlist[2].InnerText;
            }
            else
            {
                username = "";
            }
            if (xnlist[3].InnerText != null && xnlist[3].InnerText != "")
            {
                userpwd = xnlist[3].InnerText;
            }
            else
            {
                userpwd = "";
            }

        }
        else
        {
            xmlwrite();
        }

    }
    public static void xmlwrite()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement xmlelem = doc.CreateElement("", "body", "");
        doc.AppendChild(xmlelem);
        doc.LoadXml("<body></body>");
        XmlNode root = doc.SelectSingleNode("body");

        XmlElement jipwd = doc.CreateElement("jipwd");
        jipwd.InnerText = checkbox1.ToString();

        XmlElement zidong = doc.CreateElement("zidong");
        zidong.InnerText = checkbox2.ToString();

        XmlElement xusername = doc.CreateElement("username");
        xusername.InnerText = username;

        XmlElement xuserpwd = doc.CreateElement("userpwd");
        if (userpwd != null && userpwd != "")
        {
            xuserpwd.InnerText = userpwd;
        }
        else
        {
            xuserpwd.InnerText = "";
        }
        root.AppendChild(jipwd);
        root.AppendChild(zidong);
        root.AppendChild(xusername);
        root.AppendChild(xuserpwd);
        doc.Save(lujing);
    }
}