C#操作XML类

 XML转换成HTML

1.//装载xsl

XslCompiledTransform xslt = new XslCompiledTransform();

xslt.Load("output.xsl");

2.//执行转换和输出的结果文件

xslt.Transform("Company.xml","Report.html");


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

 

/// <summary>
///ETDZXML 的摘要说明
/// </summary>
public class ETDZXML
{
/// <summary>
/// 将XML对象转换成文本
/// </summary>
/// <param name="xmd">XML对象</param>
/// <returns>返回XML文件的文本字符串</returns>
public static string ToString(System.Xml.XmlDocument xmd)
{
string str = null;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Xml.XmlTextWriter tx = new System.Xml.XmlTextWriter(sw);
xmd.WriteTo(tx);
str = sw.ToString();
sw.Close();
sw = null;
tx.Close();
tx = null;
return str;
}

 

/// <summary>
/// 将文本保存成XML文件
/// </summary>
/// <param name="_xml">XML文本</param>
/// <param name="_xmlFullFilename">XML文件名</param>
public static void SaveXML(string _xml, string _xmlFullFilename)
{
System.IO.FileStream FS = new System.IO.FileStream(_xmlFullFilename, System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(FS, System.Text.Encoding.Default);
//System.Xml.XmlTextWriter myXml = new System.Xml.XmlTextWriter(FS, System.Text.Encoding.Default);
sw.Write(_xml);
sw.Close();
sw = null;
FS.Close();
FS = null;
GC.Collect();
}

 

/// <summary>
/// 读取文本文件
/// </summary>
/// <param name="_xmlRelPath">文件的相对路径</param>
/// <returns>文本字符串</returns>
public static string ReadXML(string _xmlRelPath)
{
System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(_xmlRelPath).Replace("\\xmls\\", "\\"));
string xml = sr.ReadToEnd();
sr.Close();
sr = null;
return xml;
}

 

/// <summary>
/// 读取文本文件
/// </summary>
/// <param name="_filePath">文件的绝对路径</param>
/// <returns>文本字符串</returns>
public static string ReadText(string _filePath)
{
System.IO.StreamReader sr = new System.IO.StreamReader(_filePath);
string txt = sr.ReadToEnd();
sr.Close();
sr = null;
return txt;
}

 

/// <summary>
/// 向XML文档对象插入节点及其属性
/// </summary>
/// <param name="xmd">XML文档对象(ref)</param>
/// <param name="_nodeName">节点名称</param>
/// <param name="_attrNames">属性名数组</param>
/// <param name="_attrVals">属性值数组</param>
public static void AddNode(ref System.Xml.XmlDocument xmd, string _containerNodeName, string _nodeName, string[] _attrNames, string[] _attrVals)
{

 

System.Xml.XmlElement xmeN = xmd.CreateElement("", _nodeName, "");
xmd.SelectSingleNode(_containerNodeName).AppendChild(xmeN);

 

for (int i = 0; i < _attrNames.Length; i++)
{
System.Xml.XmlAttribute xa = xmd.CreateAttribute(_attrNames[i]);
xa.InnerText = _attrVals[i];
xmeN.Attributes.Append(xa);
}
}

 

/// <summary>
/// 向XML文档对象节点(多个)插入多个子节点及其属性
/// </summary>
/// <param name="xmd">XML文档对象(ref)</param>
/// <param name="_nodeName">节点名称</param>
/// <param name="_attrNames">属性名数组</param>
/// <param name="_attrVals">属性值数组</param>
public static void AddNodes(ref System.Xml.XmlDocument xmd, string _containerNodePath, string _nodeName, string[] _attrNames, string[] _attrVals)
{

 

System.Xml.XmlElement xmeN = xmd.CreateElement("", _nodeName, "");
System.Xml.XmlNodeList xnl = xmd.SelectNodes(_containerNodePath);
xnl[xnl.Count - 1].AppendChild(xmeN);

 

for (int i = 0; i < _attrNames.Length; i++)
{
System.Xml.XmlAttribute xa = xmd.CreateAttribute(_attrNames[i]);
xa.InnerText = _attrVals[i];
xmeN.Attributes.Append(xa);
}
}
}

 

posted @ 2013-07-26 13:19  无处安放的青春  阅读(267)  评论(0编辑  收藏  举报