.net xml操作基类
来源:互联网 发布:启动和关闭mysql 编辑:程序博客网 时间:2024/05/01 05:37
using System;
using System.Xml;
namespace XmlHelper
{
/// <summary>
/// xml 的摘要说明。
/// </summary>
public class XmlHandler
{
protected XmlDocument xdoc=new XmlDocument();
public XmlElement root;
public XmlHandler()
{
}
public void LoadXml(string xml)
{
xdoc.LoadXml(xml);
root=(XmlElement)xdoc.FirstChild;
}
//取得名称为name的结点的值
public string GetValue(string name)
{
XmlNode xn=FindXnByName(root.ChildNodes,name);
if(xn==null)return null;
return xn.InnerText;
}
//创建一个包含version和指定根节点的XmlDocument
public void CreateRoot(string rootName)
{
XmlElement xe=xdoc.CreateElement(rootName);
xdoc.AppendChild(xe);
root=xe;
}
//增加一个子结点
public XmlElement AppendChild(string name,string _value)
{
return AddChild((XmlElement)root,name,_value);
}
public override string ToString()
{
return xdoc.OuterXml;
}
//为一个XmlElement添加子节点,并返回添加的子节点引用
public XmlElement AddChild(XmlElement xe,string sField,string sValue)
{
XmlElement xeTemp=xdoc.CreateElement(sField);
xeTemp.InnerText=sValue;
xe.AppendChild(xeTemp);
return xeTemp;
}
//为一个XmlElement添加子节点,并返回添加的子节点引用
protected XmlElement AddChild(XmlElement xe,XmlDocument xd,string sField)
{
XmlElement xeTemp=xd.CreateElement(sField);
xe.AppendChild(xeTemp);
return xeTemp;
}
//为一个节点添加属性
public void AddAttribute(XmlElement xe,string strName,string strValue)
{
//判断属性是否存在
string s=GetXaValue(xe.Attributes,strName);
//属性已经存在
if(s!=null)
{
throw new System.Exception("attribute exists");
}
XmlAttribute xa=xdoc.CreateAttribute(strName);
xa.Value=strValue;
xe.Attributes.Append(xa);
}
//为一个节点添加属性,不是系统表
protected void AddAttribute(XmlDocument xdoc,XmlElement xe,string strName,string strValue)
{
//判断属性是否存在
string s=GetXaValue(xe.Attributes,strName);
//属性已经存在
if(s!=null)
{
throw new Exception("Error:The attribute '"+strName+"' has been existed!");
}
XmlAttribute xa=xdoc.CreateAttribute(strName);
xa.Value=strValue;
xe.Attributes.Append(xa);
}
//通过节点名称找到指定的节点
protected XmlNode FindXnByName(XmlNodeList xnl,string strName)
{
for(int i=0;i<xnl.Count;i++)
{
if(xnl.Item(i).LocalName==strName)return xnl.Item(i);
}
return null;
}
//找到指定名称属性的值
protected string GetXaValue(XmlAttributeCollection xac,string strName)
{
for(int i=0;i<xac.Count;i++)
{
if(xac.Item(i).LocalName==strName)return xac.Item(i).Value ;
}
return null;
}
//找到指定名称属性的值
protected string GetXnValue(XmlNodeList xnl,string strName)
{
for(int i=0;i<xnl.Count;i++)
{
if(xnl.Item(i).LocalName==strName)return xnl.Item(i).InnerText;
}
return null;
}
//为一个节点指定值
protected void SetXnValue(XmlNodeList xnl,string strName,string strValue)
{
for(int i=0;i<xnl.Count;i++)
{
if(xnl.Item(i).LocalName==strName)
{
xnl.Item(i).InnerText=strValue;
return;
}
}
return;
}
//为一个属性指定值
protected void SetXaValue(XmlAttributeCollection xac,string strName,string strValue)
{
for(int i=0;i<xac.Count;i++)
{
if(xac.Item(i).LocalName==strName)
{
xac.Item(i).Value=strValue;
return;
}
}
return;
}
//寻找具有指定名称和属性/值组合的节点
protected XmlNode FindXnByXa(XmlNodeList xnl,string strXaName,string strXaValue)
{
string xa;
for(int i=0;i<xnl.Count;i++)
{
xa=GetXaValue(xnl.Item(i).Attributes,strXaName);
if(xa!=null)
{
if(xa==strXaValue)return xnl.Item(i);
}
}
return null;
}
}
}
- .net xml操作基类
- .NET 操作XML类
- Asp.Net XML操作基类
- asp.net XML操作类
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- Asp.Net XML操作基类(修改,删除,新增,创建)
- [ASP.net(C#)]XML操作类(一)
- [ASP.net(C#)]XML操作类(一)
- asp.net的XML通用操作类
- asp.net的XML通用操作类
- 我的新生活
- 中国也可以带号转网了,一年以后吧。
- 数据结构-算法: 插入排序(希尔排序法)
- 超级玛丽制作揭秘5魔法攻击 类MYANIMAGIC
- 这段时间在hp安腾上面折腾了websphere+cics,慢慢写出来和大家分享
- .net xml操作基类
- Math的 floor,round和ceil的总结
- 大端(Big Endian)与小端(Little Endian)详解
- 什么叫主动的工作
- 数据结构-排序: 交换排序(冒泡排序法)
- Ubuntu 下安装Subversion
- Acegi + Spring + Hibernate + Struts 2搭建基于角色的权限控制系统
- 删除字符串最后一个字符的几种方法
- 数据结构-排序: 交换排序(快速排序法)