XML文件操作的简单类

最近想学习一下如何操作xml文件,从网上找了些事例,然后整理了一下,弄了个不是很完整的类,希望跟大家能够一起分享一下,有什么不好的地方,还请大侠提出宝贵意见
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.IO;
None.gif
using  System.Xml;
None.gif
namespace  shyc.cdw.xml
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class XMLFile
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private XmlDocument XmlDoc;
InBlock.gif
InBlock.gif        
private String _XmlFileName;
InBlock.gif
InBlock.gif        
public String XmlFileName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _XmlFileName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _XmlFileName = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private String  _RootName;
InBlock.gif
InBlock.gif        
public String  RootName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _RootName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _RootName = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _RootText;
InBlock.gif
InBlock.gif        
public string RootText
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _RootText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _RootText = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="filename">xml文件的全名(含文件的路径)</param>

InBlock.gif        public  XMLFile(string filename)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc 
= new XmlDocument();
InBlock.gif            
this._XmlFileName = filename;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建xml文件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void CreateXMLFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlNode xmlnode;
InBlock.gif            XmlElement xmlelem;
InBlock.gif            XmlText xmltext;
InBlock.gif            XmlDoc 
= new XmlDocument();
InBlock.gif            
//加入XML的声明段落
InBlock.gif
            xmlnode = XmlDoc.CreateNode(XmlNodeType.XmlDeclaration, """");
InBlock.gif            XmlDoc.AppendChild(xmlnode);
InBlock.gif
InBlock.gif            xmlelem 
= XmlDoc.CreateElement("", _RootName, "");
InBlock.gif            xmltext 
= XmlDoc.CreateTextNode(_RootText);
InBlock.gif
InBlock.gif            xmlelem.AppendChild(xmltext);
InBlock.gif            XmlDoc.AppendChild(xmlelem);
InBlock.gif            
//保存创建好的XML文档
InBlock.gif
            XmlDoc.Save(_XmlFileName); 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加子节点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pNodeName">父节点的节点名</param>
InBlock.gif        
/// <param name="ItemName">新节点的名字</param>
InBlock.gif        
/// <param name="Content">新节点属性值的集合</param>
ExpandedSubBlockEnd.gif        
/// <param name="NodeValue">新节点的值</param>

InBlock.gif        public void AddChildNode(string pNodeName,string ItemName,string[]Content,string NodeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(pNodeName); 
InBlock.gif            
//当父节点存在的时候进行添加
InBlock.gif
            if (node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlElement elem 
= XmlDoc.CreateElement(ItemName);
InBlock.gif                
foreach (string ss in Content)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (ss != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (ss != "") elem.SetAttribute(ss.Split('=')[0], ss.Split('=')[1]);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (NodeValue != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (NodeValue != "") elem.InnerText = NodeValue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                node.AppendChild(elem);
ExpandedSubBlockEnd.gif            }

InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName,null );
InBlock.gif            writer.Formatting 
= Formatting.Indented;
InBlock.gif            XmlDoc.Save(writer);
InBlock.gif            writer.Close();
InBlock.gif            writer 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private object Exception()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif        
/// 增加根子节点
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="ItemName">新节点名称</param>
InBlock.gif       
/// <param name="Content">新节点属性集合</param>
ExpandedSubBlockEnd.gif       
/// <param name="NodeValue">新节点的值</param>

InBlock.gif        public void  AddRootChildNode(string ItemName,string[]Content,string NodeValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlNode node 
= XmlDoc.SelectSingleNode(_RootName );
InBlock.gif            XmlElement elem 
= XmlDoc.CreateElement(ItemName);
InBlock.gif            
foreach (string ss in Content)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ss != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (ss != "") elem.SetAttribute(ss.Split('=')[0], ss.Split('=')[1]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (NodeValue != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (NodeValue != "") elem.InnerText = NodeValue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            node.AppendChild(elem);
InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName, null);
InBlock.gif            writer.Formatting 
= Formatting.Indented;
InBlock.gif            XmlDoc.Save(writer);
InBlock.gif            writer.Close();
InBlock.gif            writer 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 更新某节点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
ExpandedSubBlockEnd.gif        
/// <param name="NewNode">新节点</param>

InBlock.gif        public void UpdataNode(string xPath,XmlNode NewNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName );
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(xPath); 
InBlock.gif            
if(node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                node.ParentNode.ReplaceChild(NewNode,node);
ExpandedSubBlockEnd.gif            }

InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName,null);
InBlock.gif            writer.Formatting 
= Formatting.Indented;
InBlock.gif            XmlDoc.Save(writer);
InBlock.gif            writer.Close();
InBlock.gif            writer 
= null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除某节点以及其子节点
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="xPath"></param>

InBlock.gif        public void RemoveNode(string xPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(xPath); 
InBlock.gif            
if(node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                node.ParentNode.RemoveChild(node);
ExpandedSubBlockEnd.gif            }

InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName,Encoding.UTF8 );
InBlock.gif            writer.Formatting 
= Formatting.Indented;
InBlock.gif            XmlDoc.Save(writer);
InBlock.gif            writer.Close();
InBlock.gif            writer 
= null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取指定节点的属性值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
InBlock.gif        
/// <param name="name">属性名</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public string getNodeAttribute(string xPath,string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string result = null;
InBlock.gif        
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode n 
= XmlDoc.DocumentElement.SelectSingleNode(xPath); 
InBlock.gif            
if(n != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
if(n.LocalName != "#comment" && n.Attributes[name] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(n.Attributes["active"!= null )//&& n.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        result 
= n.Attributes[name].Value;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
   
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写指定节点的属性值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
InBlock.gif        
/// <param name="name">属性名</param>
ExpandedSubBlockEnd.gif        
/// <param name="values">属性新值</param>

InBlock.gif        public void setNodeAttribute(string xPath,string name,string values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(xPath); 
InBlock.gif            
if(node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (node.LocalName != "#comment" && node.Attributes[name] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (node.Attributes["active"!= null)// && node.Attributes["active"].Value.ToUpper().Trim().Equals("TRUE"))
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        node.Attributes[name].Value 
= values;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName,Encoding.UTF8 );
InBlock.gif            writer.Formatting 
= Formatting.Indented;
InBlock.gif            XmlDoc.Save(writer);
InBlock.gif            writer.Close();
InBlock.gif            writer 
= null
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 添加指定节点的属性
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
InBlock.gif        
/// <param name="name">属性名</param>
ExpandedSubBlockEnd.gif        
/// <param name="values">属性值</param>

InBlock.gif        public void AddNodeAttribute(string xPath, string name, string values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(xPath);
InBlock.gif            
if (node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//添加新属性
InBlock.gif
                if ((!name.Equals("")) && node.Attributes[name] == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    XmlAttribute myAttribute 
= XmlDoc.CreateAttribute(name);
InBlock.gif                    myAttribute.Value 
= values;
InBlock.gif                    node.Attributes.Append(myAttribute);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName, Encoding.UTF8);
InBlock.gif            writer.Formatting 
= Formatting.Indented;
InBlock.gif            XmlDoc.Save(writer);
InBlock.gif            writer.Close();
InBlock.gif            writer 
= null;
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断是否有子节点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
ExpandedSubBlockEnd.gif        
/// <returns>子节点的个数</returns>

InBlock.gif        public int  ChildnodesNumber(string xPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(xPath);
InBlock.gif            
return node.ChildNodes.Count;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检测新增节点是否存在
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pNodeName">父节点名</param>
InBlock.gif        
/// <param name="ItemName">新节点名</param>
ExpandedSubBlockEnd.gif        
/// <returns> 110新增子节点存在 100成功 99父节点不存在 </returns>

InBlock.gif        public int NodeExist(string pNodeName,string ItemName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(pNodeName);
InBlock.gif            
if (node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int i;
InBlock.gif                
for ( i = 0; i < node.ChildNodes.Count - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (node.ChildNodes.Item(i).Name == ItemName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return 110;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return 100;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 99;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取指定节点的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
ExpandedSubBlockEnd.gif        
/// <returns>节点的值</returns>

InBlock.gif        public string getNodeValue(string xPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string result = null;
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode n 
= XmlDoc.DocumentElement.SelectSingleNode(xPath); 
InBlock.gif            
if(n != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result 
= n.InnerText;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写指定节点的值
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="xPath">节点名</param>
ExpandedSubBlockEnd.gif        
/// <param name="values">节点的新值</param>

InBlock.gif        public void  setNodeValue(string xPath,string values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDoc.Load(
this._XmlFileName);
InBlock.gif            XmlNode node 
= XmlDoc.DocumentElement.SelectSingleNode(xPath);
InBlock.gif            
if (node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                node.InnerText 
= values;
InBlock.gif                XmlTextWriter writer 
= new XmlTextWriter(this._XmlFileName, Encoding.UTF8);
InBlock.gif                writer.Formatting 
= Formatting.Indented;
InBlock.gif                XmlDoc.Save(writer);
InBlock.gif                writer.Close();
InBlock.gif                writer 
= null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
本人的QQ:280685904,期待与您的交流

转载于:https://www.cnblogs.com/chaodongwang/archive/2007/07/29/835248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值