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);
}
}
}
C#操作XML类的更多相关文章
- .NET操作Xml类
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...
- PHP DOMDocument操作 XML类 属性、方法
属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definition 以DTD或XML模式给出的节 ...
- java分享第十七天-01(封装操作xml类)
做自动化测试的人,都应该对XPATH很熟悉了,但是在用JAVA解析XML时,我们通常是一层层的遍历进去,这样的代码的局限性很大,也不方便,于是我们结合一下XPATH,来解决这个问题.所需要的JAR包: ...
- XML格式示例 与 XML操作(读取)类封装
header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...
- C#XmlHelper操作Xml文档的帮助类
using System.Xml; using System.Data; namespace DotNet.Utilities { /// <summary> /// Xml的操作公共类 ...
- [XML] C# XmlHelper操作Xml文档的帮助类 (转载)
点击下载 XmlHelper.rar 主要功能如下所示 /// <summary> /// 类说明:XmlHelper /// 编 码 人:苏飞 /// 联系方式:361983679 // ...
- C#操作Xml树的扩展类
本文提供一个操作Xml树的扩展类,与将xml字符串直接映射成实体对象的使用方法,供大家参考,学习. 下面附上源码 using System; using System.Collections.Gene ...
- C#操作xml完整类文件
C#操作xml完整类文件 xml_oper.cs using ...System; using System.Data; using System.Web; using System.Xml; /** ...
- 使用dom4j类操作xml文档
dom4j操作xml数据 1.Document对象相关 ①读取XML文件,获得document对象. SAXReader reader = new SAXReader(); Document docu ...
随机推荐
- 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
实验报告链接:http://www.cnblogs.com/lx20145332/p/6058790.html
- 对于JAVA课程的期望
对于JAVA课程的期望 我对于JAVA这门课程最初的了解可能来自于学长学姐的描述,或者是选课指南上简单的课程名称,那个时候的JAVA,对我来说遥远而又陌生,显得那么高大上,但是一转眼自己马上就要结束大 ...
- Scala之Map,Tuple
/** * 1,默认情况下Map构造的是不可变的集合,里面的内容不可修改,一旦修改就变成新的Map,原有的Map内容保持不变: * 2,Map的实例是调用工厂方法模式apply来构造Map实例,而需要 ...
- jquery源码分析
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 前段时间上班无聊之时,研究了 ...
- 自动备份SQL数据库到云存储Storage
如何自动备份SQL数据库到Storage呢. 前提条件需要SQL Server2012 SP1 CU2或更高版本 1. 备份SQL Azure数据库到云存储Storage 1)在SQL Server ...
- [BZOJ 1085][SCOI2005]骑士精神(IDA*)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1085 分析: 首先第一感觉是宽搜,但是空间需要8^15*5*5,明显不够,又鉴于最大深 ...
- ThinkPHP之MVC与URL访问
一.初探 我们在apache的www目录下创建一个文件夹,其命名为我们的应用名.然后通过入口文件生成我们的应用. 当我们用ThinkPHP创建好一个应用后,其目录结果如下所示 那么我们如何来访问我们应 ...
- angular实例教程(用来熟悉指令和过滤器的编写)
angular的插件太少了, 所以很多指令和过滤器都要自己写, 所以对指令传进来的参数, 以及angular编译的流程更加熟悉才行写出好的插件, 有些简单的指令是参考angularUI里面, 作为 ...
- Microsoft Office下载地址
文件名: cn_office_professional_plus_2016_x86_x64_dvd_6969182.iso 语言: Chinese – Simplified 文件大小:2.41 GB ...
- Windows下python的配置
Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...