• csharp: read xml


      //
                    string xmldata = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"xml涂聚文.xml";
                    XElement xe = XElement.Load(xmldata);
                    IEnumerable<XElement> element = from ele in xe.Elements("dataserver")
                                                   // where ele.Attribute("id").Value == "1"
                                               select ele;
    
                    if (element.Count() > 0)
                    {
                        List<ServerConfig> infos=showInfoByElements(element);
                    }
    
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(xmldata);
                    XmlElement xed = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.
                    string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                    XmlElement selectXe = (XmlElement)xed.SelectSingleNode(strPath);  //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.
    
                    XmlNodeList elementdd = selectXe.ChildNodes;
    
    
                    XmlNodeList senode = xed.SelectSingleNode(strPath).ChildNodes;
                    foreach (XmlNode childNode in senode)
                    {
                        XmlElement childElement = (XmlElement)childNode;
                        string ddd = childElement.InnerText;// childElement.GetAttribute("database");
                        string ddt = childNode.InnerText;
    
                    }  
                    foreach (XmlNode ee in selectXe.ChildNodes)
                    {
                      
                            string dd = ee.InnerText;                    
                    }
    
                    MessageBox.Show("連接塗聚文成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    

      

        /// <summary>
            /// 
            /// </summary>
            /// <param name="elements"></param>
            private List<ServerConfig> showInfoByElements(IEnumerable<XElement> elements)
            {
                List<ServerConfig> modelList = new List<ServerConfig>();
                List<Databases> dslist=null;
                foreach (var ele in elements)
                {
    
                    dslist = new List<Databases>();
                    ServerConfig model = new ServerConfig();
                    model.id = ele.Attribute("id").Value;// ele.Element("id").Value;
                    model.server = ele.Attribute("server").Value; //ele.Element("server").Value;
                    model.setdatabase = ele.Attribute("setdatabase").Value;// ele.Element("setdatabase").Value;
                    model.user = ele.Attribute("user").Value;
                    model.password = ele.Attribute("password").Value;
                    model.database = ele.Element("database").Value;
    
                    string xmldata = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"xmlgeovindu.xml";
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(xmldata);
                    XmlElement xed = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.
                    string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", model.id);
                    XmlElement selectXe = (XmlElement)xed.SelectSingleNode(strPath);  //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.
                    XmlNodeList elementdd = selectXe.ChildNodes;
    
                    XmlNodeList senode = xed.SelectSingleNode(strPath).ChildNodes;
                    foreach (XmlNode childNode in senode)
                    {
                        XmlElement childElement = (XmlElement)childNode;
                        string ddd = childElement.InnerText;// childElement.GetAttribute("database");
                        string ddt = childNode.InnerText;
                        Databases inf = new Databases();
                        inf.database = childElement.InnerText;
                        dslist.Add(inf);
    
                    }
                    foreach (XmlNode ee in selectXe.ChildNodes)
                    {
    
                        string dd = ee.InnerText;
                    }
    
                    model.ListData = dslist;
                    modelList.Add(model);
    
                }                
                return modelList;
            }
    

      

        /// <summary>
        /// 
        /// </summary>
        public class ServerConfig
        {
            /// <summary>
            /// 
            /// </summary>
            public string id { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string server  { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string setdatabase  { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string user  { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string password  { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string database  { get; set; }
            /// <summary>
            ///一个服务器下,多个数据 数据库名称列表
            /// </summary>
            public List<Databases> ListData { get; set; }
    
        }
        /// <summary>
        /// 数据库名称类
        /// </summary>
        public class Databases
        {
            /// <summary>
            /// 数据库名称
            /// </summary>
            public string database { get; set; }
    
        }
    

     XML:

    <?xml version="1.0" encoding="utf-8"?>
    <ServerConfig>
      <dataserver server="GEOVINDU" setdatabase="GeovinDu" user="geovindu" password="涂聚文" id="1">
        <database>塗聚文 Geovin Du</database>
      </dataserver>
    </ServerConfig>
    

     

         bool isok=ConnectionTestInfo.ConnectionTest(combServer.Text, combDatabase.Text, this.txtbUser.Text.Trim(), this.txtbPassword.Text.Trim());
                if (isok)
                {
                    string newdata = string.Empty;
                    string newserver = string.Empty;
                    //考虑没有进行添加
                    xmldatafile = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"xml涂聚文 Geovin Du.xml";
                    xmlserverfile = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"xml塗聚文Geovin Du.xml";
                   XElement xedata = XElement.Load(xmldatafile);
                   //IEnumerable<XElement> xdedata = xedata;
                   IEnumerable<XElement> element = from ele in xedata.Elements("dataserver")
                                                   where ele.Attribute("id").Value == "1"
                                                   select ele;
                   bool exists = false;
                   List<string> lis = new List<string>();
    
                   if (element.Count() > 0)
                   {
                       foreach (var ele in element)
                       {
                           if (!object.Equals(ele.Element("database"), null))
                           {
                               //lis.Add(ele.Element("database").Value);       //只查了第一条
                               XmlDocument xmlDoc = new XmlDocument();
                               xmlDoc.Load(xmldatafile);
                               XmlElement xed = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.
                               string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", ele.Attribute("id").Value); //eles.Element("server").Value
                               XmlElement selectXe = (XmlElement)xed.SelectSingleNode(strPath);  //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.
                               XmlNodeList elementdd = selectXe.ChildNodes;
                               XmlNodeList senode = xed.SelectSingleNode(strPath).ChildNodes;
                               foreach (XmlNode childNode in senode)
                               {
                                   XmlElement childElement = (XmlElement)childNode;
                                   string ddd = childElement.InnerText;// childElement.GetAttribute("database");
                                   string ddt = childNode.InnerText;
                                   lis.Add(childElement.InnerText);
                                  
    
                               }
                               foreach (XmlNode ee in selectXe.ChildNodes)
                               {
    
                                   string dd = ee.InnerText;
                               }
    
                           }
    
    
                       }
                       exists = lis.Contains(combDatabase.Text.Trim());
                       if (lis.Count > 0)
                       {                   
                               newdata = combDatabase.Text.Trim();
                               if (!exists)
                               {
                                   XmlDocument doc = new XmlDocument();
                                   doc.Load(xmldatafile);
                                   XmlNode root = doc.SelectSingleNode("ServerConfig");
                                   XmlElement xelAuthor = doc.CreateElement("database");
                                   xelAuthor.InnerText = newdata;
                                   string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                                   XmlElement xelKey = (XmlElement)doc.SelectSingleNode(strPath); //doc.SelectSingleNode("");
                                   xelKey.AppendChild(xelAuthor);
                                   doc.Save(xmldatafile);
                               }
                        }
                       else
                       {
                           XmlDocument doc = new XmlDocument();
                           doc.Load(xmldatafile);
                           XmlNode root = doc.SelectSingleNode("ServerConfig");
                           XmlElement xelAuthor = doc.CreateElement("database");
                           xelAuthor.InnerText = combDatabase.Text.Trim(); 
                           string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                           XmlElement xelKey = (XmlElement)doc.SelectSingleNode(strPath); //doc.SelectSingleNode("");
                           xelKey.AppendChild(xelAuthor);
                           doc.Save(xmldatafile);
                       }
                   }
                   else
                   {
                       newdata = combDatabase.Text.Trim();
                       XmlDocument doc = new XmlDocument();
                       doc.Load(xmldatafile);
                       XmlNode root = doc.SelectSingleNode("ServerConfig");
                       XmlElement xelAuthor = doc.CreateElement("database");
                       xelAuthor.InnerText = newdata;
                       string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                       XmlElement xelKey = (XmlElement)doc.SelectSingleNode(strPath); //doc.SelectSingleNode("");
                       xelKey.AppendChild(xelAuthor);
                       doc.Save(xmldatafile);
                   }
    
    
                   lis = new List<string>();
                    //操作
                   XElement xeserver = XElement.Load(xmlserverfile);
                   IEnumerable<XElement> eleserver = from eles in xeserver.Elements("dataserver")
                                                   where eles.Attribute("id").Value == "1"
                                                   select eles;
                   if (eleserver.Count() > 0)
                   {
                       foreach (var eles in eleserver)
                       {
                           if (!object.Equals(eles.Element("server"), null))
                           {
                              // lis.Add(eles.Element("server").Value);  //只查了第1条
                               XmlDocument xmlDoc = new XmlDocument();
                               xmlDoc.Load(xmlserverfile);
                               XmlElement xed = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.
                               string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", eles.Attribute("id").Value);   //eles.Element("server").Value
                               XmlElement selectXe = (XmlElement)xed.SelectSingleNode(strPath);  //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.
                               XmlNodeList elementdd = selectXe.ChildNodes;
                               XmlNodeList senode = xed.SelectSingleNode(strPath).ChildNodes;
                               foreach (XmlNode childNode in senode)
                               {
                                   XmlElement childElement = (XmlElement)childNode;
                                   string ddd = childElement.InnerText;// childElement.GetAttribute("database");
                                   string ddt = childNode.InnerText;
                                   lis.Add(childElement.InnerText);
    
    
                               }
                               foreach (XmlNode ee in selectXe.ChildNodes)
                               {
    
                                   string dd = ee.InnerText;
                               }
    
    
                           }
    
                       }
                       exists = false;
                       if (lis.Count > 0)
                       {
    
    
                           exists = lis.Contains(combServer.Text.Trim());
                           newserver = combServer.Text.Trim();
                           if (!exists)
                           {
                               XmlDocument doc = new XmlDocument();
                               doc.Load(xmlserverfile);
                               XmlNode root = doc.SelectSingleNode("ServerConfig");
                               XmlElement xelAuthor = doc.CreateElement("server");
                               xelAuthor.InnerText = newserver;
                               string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                               XmlElement xelKey = (XmlElement)doc.SelectSingleNode(strPath); //doc.SelectSingleNode("");
                               xelKey.AppendChild(xelAuthor);
                               doc.Save(xmlserverfile);
                           }
                       }
                       else
                       {
                           XmlDocument doc = new XmlDocument();
                           doc.Load(xmlserverfile);
                           XmlNode root = doc.SelectSingleNode("ServerConfig");
                           XmlElement xelAuthor = doc.CreateElement("server");
                           xelAuthor.InnerText = combServer.Text.Trim();
                           string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                           XmlElement xelKey = (XmlElement)doc.SelectSingleNode(strPath); //doc.SelectSingleNode("");
                           xelKey.AppendChild(xelAuthor);
                           doc.Save(xmlserverfile);
                       }
    
                   }
                   else
                   {
                       newserver = combServer.Text.Trim();
                       XmlDocument doc = new XmlDocument();
                       doc.Load(xmlserverfile);
                       XmlNode root = doc.SelectSingleNode("ServerConfig");
                       XmlElement xelAuthor = doc.CreateElement("server");
                       xelAuthor.InnerText = newserver;
                       string strPath = string.Format("/ServerConfig/dataserver[@id="{0}"]", "1");
                       XmlElement xelKey = (XmlElement)doc.SelectSingleNode(strPath); //doc.SelectSingleNode("");
                       xelKey.AppendChild(xelAuthor);
                       doc.Save(xmlserverfile);
                   }
    
    
    
                    MessageBox.Show("連i涂聚文成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("没有找到涂聚文,重来一次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    return;
                }
    
    //指定修改值
    
               SchemasNameListInfo info=model.schemaname(this.txtbUser.Text.Trim(), this.txtConnection.Text.Trim());
                SchemasNameInfo.SchemasName = info.SchemaName;
                bool ok= model.CreateFun(combDatabase.Text.ToString().Trim(),serverstr);
                if (ok)
                {
                    xmlfile = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"xml涂聚文.xml";
                   //edit xml
                    XElement xe = XElement.Load(xmlfile);
                    if (info.DatabaseName != null)
                    {
                        //dgvBookInfo.CurrentRow.Cells[1]对应着ISBN号
                        string id = "1";
                        IEnumerable<XElement> element = from ele in xe.Elements("dataserver")
                                                        where ele.Attribute("id").Value == id
                                                        select ele;
                        if (element.Count() > 0)
                        {
                            XElement first = element.First();
                            ///设置新的属性
                            first.SetAttributeValue("server", combServer.Text.ToString().Trim());
                            first.SetAttributeValue("database", combDatabase.Text.ToString().Trim());
                            first.SetAttributeValue("user", txtbUser.Text.ToString().Trim());
                            first.SetAttributeValue("password", txtbPassword.Text.ToString().Trim());
                            ///替换新的节点
                            first.ReplaceNodes(
                                      new XElement("server", combServer.Text.ToString().Trim()),
                                    new XElement("database", combDatabase.Text.ToString().Trim()),
                                      new XElement("user", txtbUser.Text.Trim()),
                                       new XElement("password", txtbPassword.Text.Trim())
                                     );
                        }
                        xe.Save(xmlfile);
                    }
             
                    MessageBox.Show("配置涂聚文成功!");
                }
                else
                {
                    MessageBox.Show("配置涂聚文不成功!");
                }
    

      

    哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
  • 相关阅读:
    时尚意
    ​runtime 运行时机制
    ios-UIAlertView,UIActionSheet-常用方法
    ios-UIActivityIndicatorView-常用方法
    ios-UIPageControl-常用方法
    ios-UISegmentedControl-常用方法
    ios-UISlider-常用方法和定时器NSTimer的使用
    ios-UITextField-常用方法
    java捕获特定异常特定处理
    ios-UIButton-常用方法
  • 原文地址:https://www.cnblogs.com/geovindu/p/15018122.html
Copyright © 2020-2023  润新知