XML 操作

本文介绍了一个用于操作XML配置文件的C#类库,包括如何读取和写入配置项及属性值。该类库提供了简便的方法来处理配置文件,避免了直接操作XML文档的复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifusing   System;   
None.gif  
using   System.Xml;   
None.gif    
None.gif  
namespace   Common   
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**////   <summary>   
InBlock.gif  
///   Config   的摘要说明。   
ExpandedSubBlockEnd.gif  
///   </summary>   

InBlock.gif  public   class   Config   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
private   String   msFileName   =   null;   
InBlock.gif    
InBlock.gif  
public   String   ConfigFile   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
get   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   this.msFileName;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
set   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
if(System.IO.File.Exists(value.Trim()))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
this.msFileName   =   value.Trim();   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
public   Config()   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
this.msFileName   =   String.Empty;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
public   Config(String   ConfigFile)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
this.ConfigFile   =   ConfigFile.Trim();   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
public   bool   ReadConfig(String   ContentName,   out   String   ContentValue)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
bool   bFlag   =   false;   
InBlock.gif    
InBlock.gif  ContentValue   
=   String.Empty;   
InBlock.gif    
InBlock.gif  
if(!System.IO.File.Exists(this.msFileName))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   bFlag;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif                          
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  System.Xml.XmlDocument   xmlDoc   
=   new   System.Xml.XmlDocument();   
InBlock.gif                                  xmlDoc.Load(
this.msFileName);   
InBlock.gif                                  System.Xml.XmlNode   xmlNode   
=   xmlDoc.SelectSingleNode(ContentName);   
InBlock.gif                                  ContentValue   
=   xmlNode.InnerText;   
InBlock.gif    
InBlock.gif                                  bFlag   
=   true;   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif                          
catch   (XmlException   xmle)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  System.Console.WriteLine(xmle.Message);   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif  
return   bFlag;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif    
InBlock.gif  
public   bool   ReadConfig(String   ContentName,   String   PropertyName,   out   String   PropertyValue)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
bool   bFlag   =   false;   
InBlock.gif    
InBlock.gif  PropertyValue   
=   String.Empty;   
InBlock.gif    
InBlock.gif  
if(!System.IO.File.Exists(this.msFileName))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   bFlag;   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif                          
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  XmlDocument   xmlDoc   
=   new   XmlDocument();   
InBlock.gif                                  xmlDoc.Load(
this.msFileName);   
InBlock.gif    
InBlock.gif                                  XmlNode   xmlNode   
=   xmlDoc.SelectSingleNode(ContentName);   
InBlock.gif    
InBlock.gif                                  XmlAttributeCollection   xmlAttr   
=   xmlNode.Attributes;   
InBlock.gif    
InBlock.gif                                  
for(int   i=0;   i<xmlAttr.Count;   ++i)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                                  
dot.gif{   
InBlock.gif                                          
if   (xmlAttr.Item(i).Name   ==   PropertyName)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                                          
dot.gif{   
InBlock.gif                                                  PropertyValue   
=   xmlAttr.Item(i).Value;   
InBlock.gif                                                  bFlag   
=   true;   
InBlock.gif                                                  
break;   
ExpandedSubBlockEnd.gif                                          }
   
ExpandedSubBlockEnd.gif                                  }
   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif                          
catch   (XmlException   xmle)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  System.Console.WriteLine(xmle.Message);   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif  
return   bFlag;   
ExpandedSubBlockEnd.gif                  }
   
InBlock.gif    
InBlock.gif                  
public   bool   WriteConfig(String   ContentName,   String   ContentValue)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{   
InBlock.gif                          
bool   bFlag   =   false;   
InBlock.gif    
InBlock.gif                          
if   (!System.IO.File.Exists(this.msFileName))   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  
return   bFlag;   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif                          
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  System.Xml.XmlDocument   xmlDoc   
=   new   System.Xml.XmlDocument();   
InBlock.gif                                  xmlDoc.Load(
this.msFileName);   
InBlock.gif                                  System.Xml.XmlNode   xmlNode   
=   xmlDoc.SelectSingleNode(ContentName);   
InBlock.gif                                  xmlNode.InnerText   
=   ContentValue;   
InBlock.gif    
InBlock.gif                                  xmlDoc.Save(
this.msFileName);   
InBlock.gif    
InBlock.gif                                  bFlag   
=   true;   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif                          
catch   (XmlException   xmle)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  System.Console.WriteLine(xmle.Message);   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif                          
return   bFlag;   
ExpandedSubBlockEnd.gif                  }
   
InBlock.gif    
InBlock.gif                  
public   bool   WriteConfig(String   ContentName,   String   PropertyName,   String   PropertyValue)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{   
InBlock.gif                          
bool   bFlag   =   false;   
InBlock.gif    
InBlock.gif                          
if   (!System.IO.File.Exists(this.msFileName))   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  
return   bFlag;   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif                          
try   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  XmlDocument   xmlDoc   
=   new   XmlDocument();   
InBlock.gif                                  xmlDoc.Load(
this.msFileName);   
InBlock.gif    
InBlock.gif                                  XmlNode   xmlNode   
=   xmlDoc.SelectSingleNode(ContentName);   
InBlock.gif    
InBlock.gif                                  XmlAttributeCollection   xmlAttr   
=   xmlNode.Attributes;   
InBlock.gif    
InBlock.gif                                  
for   (int   i   =   0;   i   <   xmlAttr.Count;   ++i)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                                  
dot.gif{   
InBlock.gif                                          
if   (xmlAttr.Item(i).Name   ==   PropertyName)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                                          
dot.gif{   
InBlock.gif                                                  xmlAttr.Item(i).Value   
=   PropertyValue;   
InBlock.gif                                                  bFlag   
=   true;   
InBlock.gif                                                  
break;   
ExpandedSubBlockEnd.gif                                          }
   
ExpandedSubBlockEnd.gif                                  }
   
InBlock.gif    
InBlock.gif                                  xmlDoc.Save(
this.msFileName);   
InBlock.gif    
InBlock.gif                                  bFlag   
=   true;   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif                          
catch   (XmlException   xmle)   
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{   
InBlock.gif                                  System.Console.WriteLine(xmle.Message);   
ExpandedSubBlockEnd.gif                          }
   
InBlock.gif    
InBlock.gif                          
return   bFlag;   
ExpandedSubBlockEnd.gif                  }
   
ExpandedSubBlockEnd.gif  }
   
ExpandedBlockEnd.gif  }

转载于:https://www.cnblogs.com/yiki/archive/2007/06/21/792041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值