using system;
using system.xml;
using system.data;
namespace message.common
{
/// <summary>
/// xml相关通用功能
/// </summary>
public class xml
{
public xml() {}
/// <summary>
/// xml资源类型
/// </summary>
public enum xmltype
{
file,
string
};
#region 读取xml资源到dataset中
/// <summary>
/// 读取xml资源到dataset中
/// </summary>
/// <param name="source">xml资源,文件为路径,否则为xml字符串</param>
/// <param name="xmltype">xml资源类型</param>
/// <returns>dataset</returns>
public static dataset getdataset(string source, xmltype xmltype)
{
dataset ds = new dataset();
if (xmltype == xmltype.file)
{
ds.readxml(source);
}
else
{
xmldocument xd = new xmldocument();
xd.loadxml(source);
xmlnodereader xnr = new xmlnodereader(xd);
ds.readxml(xnr);
}
return ds;
}
#endregion
#region 获取一个字符串xml文档中的ds
///<remarks>
///赵洪
///</remarks>
/// <summary>
/// 获取一个字符串xml文档中的ds
/// </summary>
/// <param name="xml_string">含有xml信息的字符串</param>
public static void get_xmlvalue_ds(string xml_string,ref dataset ds)
{
system.xml.xmldocument xd= new xmldocument();
xd.loadxml(xml_string);
xmlnodereader xnr= new xmlnodereader(xd);
ds.readxml(xnr);
xnr.close();
int a= ds.tables.count;
}
#endregion
#region 读取xml资源到datatable中
/// <summary>
/// 读取xml资源到datatable中
/// </summary>
/// <param name="source">xml资源,文件为路径,否则为xml字符串</param>
/// <param name="xmltype">xml资源类型:文件,字符串</param>
/// <param name="tablename">表名称</param>
/// <returns>datatable</returns>
public static datatable gettable(string source, xmltype xmltype, string tablename)
{
dataset ds = new dataset();
if (xmltype == xmltype.file)
{
ds.readxml(source);
}
else
{
xmldocument xd = new xmldocument();
xd.loadxml(source);
xmlnodereader xnr = new xmlnodereader(xd);
ds.readxml(xnr);
}
return ds.tables[tablename];
}
#endregion
#region 读取xml资源中指定的datatable的指定行指定列的值
/// <summary>
/// 读取xml资源中指定的datatable的指定行指定列的值
/// </summary>
/// <param name="source">xml资源</param>
/// <param name="xmltype">xml资源类型:文件,字符串</param>
/// <param name="tablename">表名</param>
/// <param name="rowindex">行号</param>
/// <param name="colname">列名</param>
/// <returns>值,不存在时返回null</returns>
public static object gettablecell(string source, xmltype xmltype, string tablename, int rowindex, string colname)
{
dataset ds = new dataset();
if (xmltype == xmltype.file)
{
ds.readxml(source);
}
else
{
xmldocument xd = new xmldocument();
xd.loadxml(source);
xmlnodereader xnr = new xmlnodereader(xd);
ds.readxml(xnr);
}
return ds.tables[tablename].rows[rowindex][colname];
}
#endregion
#region 读取xml资源中指定的datatable的指定行指定列的值
/// <summary>
/// 读取xml资源中指定的datatable的指定行指定列的值
/// </summary>
/// <param name="source">xml资源</param>
/// <param name="xmltype">xml资源类型:文件,字符串</param>
/// <param name="tablename">表名</param>
/// <param name="rowindex">行号</param>
/// <param name="colindex">列号</param>
/// <returns>值,不存在时返回null</returns>
public static object gettablecell(string source, xmltype xmltype, string tablename, int rowindex, int colindex)
{
dataset ds = new dataset();
if (xmltype == xmltype.file)
{
ds.readxml(source);
}
else
{
xmldocument xd = new xmldocument();
xd.loadxml(source);
xmlnodereader xnr = new xmlnodereader(xd);
ds.readxml(xnr);
}
return ds.tables[tablename].rows[rowindex][colindex];
}
#endregion
#region 获取一个字符串xml文档中的一个table,指定行,指定列的值
/// <summary>
/// 获取一个字符串xml文档中的一个table,指定行,指定列的值
/// </summary>
/// <param name="xml_string">含有xml信息的字符串</param>
/// <param name="tablename">表名</param>
/// <param name="row_index">指定行</param>
/// <param name="col_name">指定列名</param>
/// <returns>相应节点的值</returns>
public static string get_xmlvalue(string xml_string,string tablename,int row_index,string col_name)
{
system.xml.xmldocument xd= new xmldocument();
xd.loadxml(xml_string);
xmlelement root = xd.documentelement;
xmlnode xe= root.clonenode(false);
xe = root.selectnodes(tablename).item(row_index);
string val="";
if(null==xe)
{
return "";
}
foreach(xmlnode xn in xe.childnodes )
{
if(xn.localname==col_name)
{
val = xn.innertext;
break;
}
}
return val;
}
/// <summary>
/// 获取一个xml文件中的一个table,指定行,指定列的值
/// </summary>
/// <param name="xml_string">含有xml信息的字符串</param>
/// <param name="tablename">表名</param>
/// <param name="row_index">指定行</param>
/// <param name="col_name">指定列名</param>
/// <returns>相应节点的值</returns>
public static string get_xmlvalue(string xml_string,string tablename,int row_index,string col_name,bool isfile)
{
system.xml.xmldocument xd= new xmldocument();
if(isfile)
xd.load(xml_string);
else
xd.loadxml(xml_string);
xmlelement root = xd.documentelement;
xmlnode xe= root.clonenode(false);
xe = root.selectnodes(tablename).item(row_index);
string val="";
if(null==xe)
{
return "";
}
foreach(xmlnode xn in xe.childnodes )
{
if(xn.localname==col_name)
{
val = xn.innertext;
break;
}
}
return val;
}
#endregion
#region 获取一个字符串xml文档中的dt
///<remarks>
///
///</remarks>
/// <summary>
/// 获取一个字符串xml文档中的dt
/// </summary>
/// <param name="xml_string">含有xml信息的字符串</param>
public static void get_xmlvalue_dt(string xml_string,ref datatable dt,string table_name)
{
dataset ds = new dataset();
system.xml.xmldocument xd= new xmldocument();
xd.loadxml(xml_string);
//system.io.filestream fs = new system.io.filestream("c://aa.xml",system.io.filemode.open);
//xd.save(fs);
// system.xml.xmlreader xr=(system.xml.xmlreader)((object)sr);
//ds=new dataset();
xmlnodereader xnr= new xmlnodereader(xd);
ds.readxml(xnr);
xnr.close();
dt = ds.tables[table_name];
}
#endregion
#region 将datatable写入xml文件中
/// <summary>
/// 将datatable写入xml文件中
/// </summary>
/// <param name="dt">含有数据的datatable</param>
/// <param name="filepath">文件路径</param>
public static void savetabletofile(datatable dt, string filepath)
{
dataset ds = new dataset("config");
ds.tables.add(dt.copy());
ds.writexml(filepath);
}
#endregion
#region 将datatable以指定的根结点名称写入文件
/// <summary>
/// 将datatable以指定的根结点名称写入文件
/// </summary>
/// <param name="dt">含有数据的datatable</param>
/// <param name="rootname">根结点名称</param>
/// <param name="filepath">文件路径</param>
public static void savetabletofile(datatable dt, string rootname, string filepath)
{
dataset ds = new dataset(rootname);
ds.tables.add(dt.copy());
ds.writexml(filepath);
}
#endregion
#region 使用dataset方式更新xml文件节点
/// <summary>
/// 使用dataset方式更新xml文件节点
/// </summary>
/// <param name="filepath">xml文件路径</param>
/// <param name="tablename">表名称</param>
/// <param name="rowindex">行号</param>
/// <param name="colname">列名</param>
/// <param name="content">更新值</param>
/// <returns>更新是否成功</returns>
public static bool updatetablecell(string filepath, string tablename, int rowindex, string colname, string content)
{
bool flag = false;
dataset ds = new dataset();
ds.readxml(filepath);
datatable dt = ds.tables[tablename];
if (dt.rows[rowindex][colname] != null)
{
dt.rows[rowindex][colname] = content;
ds.writexml(filepath);
flag = true;
}
else
{
flag = false;
}
return flag;
}
#endregion
#region 使用dataset方式更新xml文件节点
/// <summary>
/// 使用dataset方式更新xml文件节点
/// </summary>
/// <param name="filepath">xml文件路径</param>
/// <param name="tablename">表名称</param>
/// <param name="rowindex">行号</param>
/// <param name="colindex">列号</param>
/// <param name="content">更新值</param>
/// <returns>更新是否成功</returns>
public static bool updatetablecell(string filepath, string tablename, int rowindex, int colindex, string content)
{
bool flag = false;
dataset ds = new dataset();
ds.readxml(filepath);
datatable dt = ds.tables[tablename];
if (dt.rows[rowindex][colindex] != null)
{
dt.rows[rowindex][colindex] = content;
ds.writexml(filepath);
flag = true;
}
else
{
flag = false;
}
return flag;
}
#endregion
#region 读取xml资源中的指定节点内容
/// <summary>
/// 读取xml资源中的指定节点内容
/// </summary>
/// <param name="source">xml资源</param>
/// <param name="xmltype">xml资源类型:文件,字符串</param>
/// <param name="nodename">节点名称</param>
/// <returns>节点内容</returns>
public static object getnodevalue(string source, xmltype xmltype, string nodename)
{
xmldocument xd = new xmldocument();
if (xmltype == xmltype.file)
{
xd.load(source);
}
else
{
xd.loadxml(source);
}
xmlelement xe = xd.documentelement;
xmlnode xn = xe.selectsinglenode("//" + nodename);
if(xn != null)
{
return xn.innertext;
}
else
{
return null;
}
}
/// <summary>
/// 读取xml资源中的指定节点内容
/// </summary>
/// <param name="source">xml资源</param>
/// <param name="nodename">节点名称</param>
/// <returns>节点内容</returns>
public static object getnodevalue(string source,string nodename)
{
if(source == null || nodename == null || source == "" || nodename == "" || source.length < nodename.length * 2)
{
return null;
}
else
{
int start = source.indexof("<" + nodename + ">") + nodename.length + 2;
int end = source.indexof("</" + nodename + ">");
if(start == -1 || end == -1)
{
return null;
}
else if(start >= end)
{
return null;
}
else
{
return source.substring(start,end - start);
}
}
}
#endregion
#region 更新xml文件中的指定节点内容
/// <summary>
/// 更新xml文件中的指定节点内容
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="nodename">节点名称</param>
/// <param name="nodevalue">更新内容</param>
/// <returns>更新是否成功</returns>
public static bool updatenode(string filepath, string nodename, string nodevalue)
{
bool flag = false;
xmldocument xd = new xmldocument();
xd.load(filepath);
xmlelement xe = xd.documentelement;
xmlnode xn = xe.selectsinglenode("//" + nodename);
if (xn != null)
{
xn.innertext = nodevalue;
flag = true;
}
else
{
flag = false;
}
return flag;
}
#endregion
#region 操作xml文件中指定节点的数据
/// <summary>
/// 获得xml文件中指定节点的节点数据
/// </summary>
/// <param name="tablename"></param>
/// <returns></returns>
public static string getnodeinfobynodename(string path,string nodename)
{
string xmlstring="";
xmldocument xml=new xmldocument();
xml.load(path);
system.xml.xmlelement root=xml.documentelement;
system.xml.xmlnode node=root.selectsinglenode("//"+nodename);
if (node!=null)
{
xmlstring=node.innertext;
}
return xmlstring;
}
#endregion
/// <summary>
/// 根据xml路径获取dataset。如果table名为空:flag=false 返回所有xml的数据;flag=true 将xml中的table名作为数据项返回。否则根据table名获取相应的table信息返回。
/// </summary>
/// <param name="xmlpath">xml文件路径</param>
/// <param name="tablename">所要获取的table名,可为空</param>
/// <param name="flag">若为true,则只将所有表名作为数据项返回;若为false,则返回所要获取的table的所有数据</param>
/// <returns>返回所获取的dataset</returns>
/// <summary>
/// 根据xml路径获取dataset。如果table名为空:flag=false 返回所有xml的数据;flag=true 将xml中的table名作为数据项返回。否则根据table名获取相应的table信息返回。
/// </summary>
/// <param name="xmlpath">xml文件路径</param>
/// <param name="tablename">所要获取的table名,可为空</param>
/// <param name="flag">若为true,则只将所有表名作为数据项返回;若为false,则返回所要获取的table的所有数据</param>
/// <returns>返回所获取的dataset</returns>
public static dataset gettablebyxml(string xmlpath,string tablename,bool flag)
{
dataset ds=new dataset();
if (tablename=="")
{
dataset ds1=new dataset();
ds1.readxml(xmlpath);
if (ds1.tables.count>0)
{
if (flag)
{
datatable dt=new datatable("typetable");
dt.columns.add("tablename",typeof(string));
ds.tables.add(dt);
for (int i=0;i<ds1.tables.count;i++)
{
datarow dr=dt.newrow();
dr["tablename"]=ds1.tables[i].tablename;
ds.tables["typetable"].rows.add(dr);
}
}
else
{
ds=ds1.copy();
}
}
}
else
{
dataset ds2=new dataset();
ds2.readxml(xmlpath);
if (ds2.tables[tablename]!=null)
{
ds.tables.add(ds2.tables[tablename].copy());
}
}
return ds;
}
/// <summary>
/// escape invalid unicode in xml
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string replaceinvalid(string str)
{
system.text.regularexpressions.regex r = new system.text.regularexpressions.regex("[/x00-/x08|/x0b-/x0c|/x0e-/x1f]");
return r.replace(str," ");
}
/// <summary>
/// 获得接口错误信息
/// </summary>
/// <param name="errcode">错误编码</param>
/// <returns></returns>
public static string getinterfaceerrorstring(string errcode)
{
system.text.stringbuilder sb = new system.text.stringbuilder();
sb.append("<?xml version=/"1.0/" encoding=/"gb2312/"?>");
sb.append("<root>");
sb.append("<result><return_result>"+errcode+"</return_result></result>");
sb.append("</root>");
return sb.tostring();
}
}
}