• C#读取XML节点内容方法实例简析(ZZ)


    #读取XML节点内容方法实例入手让我们来看看C#读取XML节点的实现:
    1. using    System;     
    2.    using    System.Xml;     
    3.    using    System.Xml.XPath;     
    4.    using    System.Data;     
    5.       
    6.    class    ReadXML     
    7.    {     
    8.        public    static    void    Main()     
    9.        {     
    10.    string    sFile    =    "ReadXml.xml";     
    11.       
    12.    //C#读取XML节点method    1     
    13.    XmlDocument    doc    =    new    XmlDocument();     
    14.    doc.Load(sFile);     
    15.    XmlNode    node    =    doc.DocumentElement["News"]["Content"];     
    16.    Console.WriteLine(node.InnerText);     
    17.       
    18.    //C#读取XML节点method2     
    19.    node    =    doc.SelectSingleNode("//Content");     
    20.    Console.WriteLine(node.InnerText);     
    21.       
    22.    //similarly     
    23.    node    =    doc.DocumentElement.SelectSingleNode("News/Content");     
    24.    Console.WriteLine(node.InnerText);     
    25.       
    26.    //C#读取XML节点method    3     
    27.    DataSet    ds    =    new    DataSet();     
    28.    ds.ReadXml(sFile);     
    29.    Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());     
    30.       
    31.    //C#读取XML节点method    4     
    32.    XmlTextReader    reader    =    new    XmlTextReader(sFile);     
    33.    while    (reader.Read())     
    34.    {     
    35.    if    (reader.Name    ==    "Content")     
    36.    {     
    37.    Console.WriteLine("***"    +    reader.ReadString());     
    38.    break;     
    39.    }     
    40.    }     
    41.       
    42.    reader.Close();     
    43.       
    44.    //C#读取XML节点method    5     
    45.       
    46.    XPathDocument    xpdoc    =    new    XPathDocument(sFile);     
    47.    XPathNavigator    xpnv    =    xpdoc.CreateNavigator();     
    48.    xpnv.MoveToFirstChild();     
    49.    xpnv.MoveToFirstChild();     
    50.    xpnv.MoveToFirstChild();     
    51.    xpnv.MoveToNext();xpnv.MoveToNext();xpnv.MoveToNext();     
    52.    Console.WriteLine("pathnavigator:"    +    xpnv.Value);     
    53.        }     
    54.    } 
     
     
    我的方法
    string newQiChar=DateTime.Now .ToString ("yyMMdd");    
        string phyPath = AppDomain.CurrentDomain.BaseDirectory;    
        string name = "config.xml";
        string configPath = Path.Combine(phyPath, name); 
     SaveConfigXml (configPath,newQi,newQiChar);
    /// <summary>
      /// 保存配置文件
      /// </summary>
      /// <param name="savePath">文件路径</param>
      /// <param name="qishu">int型期数</param>
      /// <param name="qishuChar">字符串型期数</param>
      /// <returns></returns>
      public static bool SaveConfigXml(string savePath,int qishu,string qishuChar)
      {
       bool flag = false;
       try
       {
        XmlDocument doc = new XmlDocument();
        bool exists = System.IO.File.Exists(savePath);
        if (!exists)
        {//生成        
         doc.LoadXml("<Configs></Configs>");               //创建根结点  MaxQishu   
         XmlNode root = doc.SelectSingleNode("Configs");
         XmlElement MaxQishu = doc.CreateElement("MaxQishu");
         MaxQishu.SetAttribute("id", "qishu");
         MaxQishu.SetAttribute("value", qishu.ToString());
         MaxQishu.SetAttribute("text", qishuChar);
         root.AppendChild(MaxQishu);
         doc.Save(savePath);
        }
        else
        {//修改
         doc.Load(savePath);
         XmlElement xe = (XmlElement)doc.SelectSingleNode("Configs/MaxQishu");
         if (xe != null)
         {
          xe.SetAttribute("id", "qishu");
          xe.SetAttribute("value", qishu.ToString());
          xe.SetAttribute("text", qishuChar);
          doc.Save(savePath); //保存
         }
        }
        flag = true;
       }
       catch (Exception ex)
       {
        flag = false;
       }
       return flag;
      }
     
      /// <summary>
      /// 从配置文件中读取期数
      /// </summary>
      /// <param name="savePath"></param>
      /// <param name="type">1-int数字类型期数,0-字符串类型期数</param>
      /// <returns></returns>
      public static object ReadMaxQishuFromXml(string savePath, int type)
      {
       object result = string.Empty;
       try
       {
        XmlDocument doc = new XmlDocument();
        bool exists = System.IO.File.Exists(savePath);
        if (exists)
        {//存在        
         doc.Load(savePath);
         #region
         /*
         string xpathPath = "";
         if (type == 1)
         {//数字期数
          xpathPath = "Configs/MaxQishu/value";
         }
         else
         {
          xpathPath = "Configs/MaxQishu/text";
         }
         XmlNode node = (XmlNode)doc.SelectSingleNode(xpathPath);
         result = node.InnerText;
          * */
         #endregion
                  
         XmlElement xe = (XmlElement)doc.SelectSingleNode("Configs/MaxQishu");
         string name = xe.Name;
         string value = xe.Value;
         if (type == 1)
         {
          result = xe.GetAttribute("value");
         }
         else
         {
          result = xe.GetAttribute("text");
         }    
        }
       }
       catch (Exception ex)
       {
       }
       return result;
      }
     }
  • 相关阅读:
    体检套餐管理系统
    Altium Designer9.4局域网内冲突的问题
    关于ARM Linux下的SD卡及U盘的挂载问题
    mkimage command not found – U-Boot images will not be built
    Xilinx------BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用
    linux文件压缩解压命令
    VIVADO 入门之仿真与逻辑分析仪使用
    Linux自动运行应用程序
    ZYNQ学习之二-EMIO
    inux grep 命令 搜索含有"zynq"字符的文件
  • 原文地址:https://www.cnblogs.com/zhangjun1130/p/1887108.html
Copyright © 2020-2023  润新知