读取 XML类 (XmlHelper)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace System.Bwch.XmlConfig
{
/**////
/// 读取XML配置文件类
///
public class XmlHelper
{
private string strXmlPath = ""; //Xml文档路径
private XmlDocument xmlDoc; //XML文档
/**////
/// 初始化ReadXml类
///
/// XML文件路径
public XmlHelper(string XMLPath)
{
if (!System.IO.File.Exists(XMLPath))
{
throw new Exception("没有找到指定的路径:" + XMLPath + "的XML文档");
}
strXmlPath = XMLPath;
xmlDoc = new XmlDocument();
xmlDoc.Load(XMLPath);
}
/**////
/// 读取XML文件指定键值的value 值
///
/// 键值的路径,格式为(根节点/节点/子节点)
/// 指定键值的属性名称
/// value值
public string ReadXmlValue(string XMLNodePath,string valueName)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XMLNodePath);
return xml.GetAttribute(valueName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**////
/// 写XML指定节点的属性
///
/// 键值路径,格式为((根节点/节点/子节点))
/// 属性名称
/// 属性
///
public bool WriteXmlValue(string XmlNodePath,string valueName, string Value)
{
try
{
XmlElement xml = (XmlElement)xmlDoc.SelectSingleNode(XmlNodePath);
xml.SetAttribute(valueName, Value); //设置
xmlDoc.Save(strXmlPath); //保存
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/**//
相关文档:
Definition comparer class,
class ItemComparer : IEqualityComparer<XElement>
{
public bool Equals(XElement x, XElement y)
{
return x.Attribute("Name").Value == x.Attribute("Name").Value;
}
public int GetHashCode(XElement obj)
......
public int createXMLFile(String filename) {
int returnValue = 0;
Document document = DocumentHelper.createDocument(); //生成Document,用于管理XML文档
Element booksElement = document.addElement("books"); //添加 ......
XML文件实例:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource auth="Container" maxActive="20" name="sss" password="123"
type="javax.sql.DataSource" />
<Resource auth="Container" ......
最常见的XML数据类型有:Element, Attribute,Comment, Text.
Element, 指形如<Name>Tom<Name>的节点。它可以包括:Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference.
Attribute, 指在<Employee id=”12345”>中的粗体部分。
&nb ......
package com.jcauto.action;
import java.util.ArrayList;
import java.util.List;
public class ContentRsp {
private String resultCode;
List<ContentInfo> contentList = new ArrayList<ContentInfo>();
public void addContent(ContentInfo contentInfo) {
contentList.add(contentI ......