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);
}
}
}

 

转载于:https://www.cnblogs.com/taomylife/p/3216899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值