• C# XML文件操作


    XML文件内容样例

    <?xml version="1.0" encoding="utf-8"?>
    <DataBaseInfo>
      <Ad>
        <ServerName>172.16.78.198</ServerName>
        <UserName>sa</UserName>
        <UserPwd>1</UserPwd>
        <DataBase>dcdemo</DataBase>
      </Ad>
    </DataBaseInfo>

    创建XML文件,写入XML文件,读取XML文件

            /// <summary>
            
    /// 创建XML文件
            
    /// </summary>
            
    /// <param name="filename"></param>
            public static void CreateXmlFile(string filename, string Server, string User, string Pwd, string DataBase)
            {
                XmlDocument xmldoc = new XmlDocument();
                XmlNode node;
                node = xmldoc.CreateXmlDeclaration("1.0""utf-8"null);
                xmldoc.AppendChild(node);
                //创建根节点
                XmlNode root = xmldoc.CreateElement("DataBaseInfo");
                xmldoc.AppendChild(root);

                XmlNode node1 = xmldoc.CreateNode(XmlNodeType.Element, "Ad"null);
                CreateNode(xmldoc, node1, "ServerName", Server);
                CreateNode(xmldoc, node1, "UserName", User);
                CreateNode(xmldoc, node1, "UserPwd", Pwd);
                CreateNode(xmldoc, node1, "DataBaee", DataBase);
                root.AppendChild(node1);
                try
                {
                    xmldoc.Save(filename);

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            
    /// 写入XML文件
            
    /// </summary>
            
    /// <param name="file"></param>
            public static void WriteXmlFile(string file, string Server, string User, string Pwd, string DataBase)
            {
                XmlDocument xmdoc = new XmlDocument();
                try
                {
                    xmdoc.Load(file);
                    XmlNode root = xmdoc.SelectSingleNode("DataBaseInfo");

                    if (root != null)
                    {
                        XmlNode node = xmdoc.CreateNode(XmlNodeType.Element, "Ad"null);
                        CreateNode(xmdoc, node, "ServerName", Server);
                        CreateNode(xmdoc, node, "UserName", User);
                        CreateNode(xmdoc, node, "UserPwd", Pwd);
                        CreateNode(xmdoc, node, "DataBaee", DataBase);
                        root.AppendChild(node);
                    }
                    xmdoc.Save(file);

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            
    /// 创建节点
            
    /// </summary>
            
    /// <param name="xmldoc"></param>
            
    /// <param name="parentnode"></param>
            
    /// <param name="name"></param>
            
    /// <param name="value"></param>
            public static void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value)
            {
                XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null);
                node.InnerText = value;
                parentnode.AppendChild(node);
            }

            /// <summary>
            
    /// 获取登录到数据库的用户名
            
    /// </summary>
            public static string GetDBUserName(string DataBaseInfo)
            {
                //string DataBaseInfo = Application.StartupPath;
                DataBaseInfo = DataBaseInfo + "\\DataBase.xml";
                string dbUser = "";
                if (File.Exists(DataBaseInfo))
                {
                    XmlDocument xmdoc = new XmlDocument();
                    xmdoc.Load(DataBaseInfo);
                    //搜索指定的节点
                    System.Xml.XmlNodeList nodes = xmdoc.SelectNodes("//Ad");
                    if (nodes != null)
                    {
                        foreach (System.Xml.XmlNode xn in nodes)
                        {
                            dbUser = xn.SelectSingleNode("UserName").InnerText.ToString();
                        }
                    }
                }
                return dbUser;

            }
             /// <summary>
             
    /// 读取XML文档
             
    /// </summary>
            public static DataTable GetSelectedProduceInfo(string produceInfo, string produceNum)
            {
                //string produceInfo = Application.StartupPath;
                produceInfo = produceInfo + "\\ProduceInfo.xml";

                DataTable dt = new DataTable();
                if (File.Exists(produceInfo))
                {
                    XmlDocument xmdoc = new XmlDocument();
                    xmdoc.Load(produceInfo);

                    DataRow dr;
                    dt.Columns.Add("ProduceNum"typeof(string));//产品编号
                    dt.Columns.Add("ProduceName"typeof(string));//产品名称
                    dt.Columns.Add("ProduceSelect"typeof(string));//是否被选择
                    dt.Columns.Add("ProduceSelectStart"typeof(string));//是否默认启动
                    dt.Columns.Add("ProducePath"typeof(string));

                    dt.Columns.Add("ProduceModuleStart"typeof(string));
                    dr = dt.NewRow();

                    System.Xml.XmlNodeList nodes = xmdoc.SelectNodes("//Ad");
                    if (nodes != null)
                    {
                        foreach (System.Xml.XmlNode xn in nodes)
                        {
                            if (xn.SelectSingleNode("ProduceNum").InnerText.ToString() == produceNum)
                            {
                                dr[0] = xn.SelectSingleNode("ProduceNum").InnerText.ToString();
                                dr[1] = xn.SelectSingleNode("ProduceName").InnerText.ToString();
                                dr[2] = xn.SelectSingleNode("ProduceSelect").InnerText.ToString();
                                dr[3] = xn.SelectSingleNode("ProduceSelectStart").InnerText.ToString();
                                dr[4] = xn.SelectSingleNode("ProducePath").InnerText.ToString();
                                dr[5] = xn.SelectSingleNode("ProduceModuleStart").InnerText.ToString();

                                if (dr != null)
                                {
                                    dt.Rows.Add(dr);
                                }
                                break;
                            }
                        }
                    }
                }
                return dt;
            }
  • 相关阅读:
    [导入]流失
    [导入]喜欢与爱
    [导入]毕业的大学生的100条忠告
    sql 实现从右边查询制定字符出现的位置
    [导入]毕业了
    gridview 实现多字段综合查询
    Java实现图片添加水印文字(左右下角)
    python安装win32api pywin32 后出现 ImportError: DLL load failed
    Asp.net mvc 速度优化 关注Yslow(1)
    WPF 3D中如何选择模型的一部分并维护模型
  • 原文地址:https://www.cnblogs.com/lanshy/p/3067917.html
Copyright © 2020-2023  润新知