zoukankan      html  css  js  c++  java
  • XML 操作

    using   System;   
      
    using   System.Xml;   
        
      
    namespace   Common   
      
    {   
      
    ///   <summary>   
      
    ///   Config   的摘要说明。   
      
    ///   </summary>   

      public   class   Config   
      
    {   
      
    private   String   msFileName   =   null;   
        
      
    public   String   ConfigFile   
      
    {   
      
    get   
      
    {   
      
    return   this.msFileName;   
      }
       
      
    set   
      
    {   
      
    if(System.IO.File.Exists(value.Trim()))   
      
    {   
      
    this.msFileName   =   value.Trim();   
      }
       
      }
       
      }
       
        
      
    public   Config()   
      
    {   
      
    this.msFileName   =   String.Empty;   
      }
       
        
      
    public   Config(String   ConfigFile)   
      
    {   
      
    this.ConfigFile   =   ConfigFile.Trim();   
      }
       
        
      
    public   bool   ReadConfig(String   ContentName,   out   String   ContentValue)   
      
    {   
      
    bool   bFlag   =   false;   
        
      ContentValue   
    =   String.Empty;   
        
      
    if(!System.IO.File.Exists(this.msFileName))   
      
    {   
      
    return   bFlag;   
      }
       
        
                              
    try   
                              
    {   
                                      System.Xml.XmlDocument   xmlDoc   
    =   new   System.Xml.XmlDocument();   
                                      xmlDoc.Load(
    this.msFileName);   
                                      System.Xml.XmlNode   xmlNode   
    =   xmlDoc.SelectSingleNode(ContentName);   
                                      ContentValue   
    =   xmlNode.InnerText;   
        
                                      bFlag   
    =   true;   
                              }
       
                              
    catch   (XmlException   xmle)   
                              
    {   
                                      System.Console.WriteLine(xmle.Message);   
                              }
       
        
      
    return   bFlag;   
      }
       
        
      
    public   bool   ReadConfig(String   ContentName,   String   PropertyName,   out   String   PropertyValue)   
      
    {   
      
    bool   bFlag   =   false;   
        
      PropertyValue   
    =   String.Empty;   
        
      
    if(!System.IO.File.Exists(this.msFileName))   
      
    {   
      
    return   bFlag;   
                              }
       
        
                              
    try   
                              
    {   
                                      XmlDocument   xmlDoc   
    =   new   XmlDocument();   
                                      xmlDoc.Load(
    this.msFileName);   
        
                                      XmlNode   xmlNode   
    =   xmlDoc.SelectSingleNode(ContentName);   
        
                                      XmlAttributeCollection   xmlAttr   
    =   xmlNode.Attributes;   
        
                                      
    for(int   i=0;   i<xmlAttr.Count;   ++i)   
                                      
    {   
                                              
    if   (xmlAttr.Item(i).Name   ==   PropertyName)   
                                              
    {   
                                                      PropertyValue   
    =   xmlAttr.Item(i).Value;   
                                                      bFlag   
    =   true;   
                                                      
    break;   
                                              }
       
                                      }
       
                              }
       
                              
    catch   (XmlException   xmle)   
                              
    {   
                                      System.Console.WriteLine(xmle.Message);   
                              }
       
        
      
    return   bFlag;   
                      }
       
        
                      
    public   bool   WriteConfig(String   ContentName,   String   ContentValue)   
                      
    {   
                              
    bool   bFlag   =   false;   
        
                              
    if   (!System.IO.File.Exists(this.msFileName))   
                              
    {   
                                      
    return   bFlag;   
                              }
       
        
                              
    try   
                              
    {   
                                      System.Xml.XmlDocument   xmlDoc   
    =   new   System.Xml.XmlDocument();   
                                      xmlDoc.Load(
    this.msFileName);   
                                      System.Xml.XmlNode   xmlNode   
    =   xmlDoc.SelectSingleNode(ContentName);   
                                      xmlNode.InnerText   
    =   ContentValue;   
        
                                      xmlDoc.Save(
    this.msFileName);   
        
                                      bFlag   
    =   true;   
                              }
       
                              
    catch   (XmlException   xmle)   
                              
    {   
                                      System.Console.WriteLine(xmle.Message);   
                              }
       
        
                              
    return   bFlag;   
                      }
       
        
                      
    public   bool   WriteConfig(String   ContentName,   String   PropertyName,   String   PropertyValue)   
                      
    {   
                              
    bool   bFlag   =   false;   
        
                              
    if   (!System.IO.File.Exists(this.msFileName))   
                              
    {   
                                      
    return   bFlag;   
                              }
       
        
                              
    try   
                              
    {   
                                      XmlDocument   xmlDoc   
    =   new   XmlDocument();   
                                      xmlDoc.Load(
    this.msFileName);   
        
                                      XmlNode   xmlNode   
    =   xmlDoc.SelectSingleNode(ContentName);   
        
                                      XmlAttributeCollection   xmlAttr   
    =   xmlNode.Attributes;   
        
                                      
    for   (int   i   =   0;   i   <   xmlAttr.Count;   ++i)   
                                      
    {   
                                              
    if   (xmlAttr.Item(i).Name   ==   PropertyName)   
                                              
    {   
                                                      xmlAttr.Item(i).Value   
    =   PropertyValue;   
                                                      bFlag   
    =   true;   
                                                      
    break;   
                                              }
       
                                      }
       
        
                                      xmlDoc.Save(
    this.msFileName);   
        
                                      bFlag   
    =   true;   
                              }
       
                              
    catch   (XmlException   xmle)   
                              
    {   
                                      System.Console.WriteLine(xmle.Message);   
                              }
       
        
                              
    return   bFlag;   
                      }
       
      }
       
      }
  • 相关阅读:
    javascript 将中文符号转换成英文符号
    js 函数定义的2种方式
    javascript 面向对象编程(工厂模式、构造函数模式、原型模式)
    10.2.0.4 to 10.2.0.5 Installation of Patch Set Release (Windows)
    流接口驱动程序的工作原理和开发步骤嵌入式
    android开发面试题
    ORACLE-014:oracle中查看DBLinkpassword
    Java相关知识(一)
    Android 中模仿 Twitter 实现 Toolbar Indicator
    使用excel进行数据挖掘(3)----类别检測
  • 原文地址:https://www.cnblogs.com/yiki/p/792041.html
Copyright © 2011-2022 走看看