@H_
404_0@using Sy
stem;
using Sy
stem.Xml;
using Sy
stem.Con
figuration;
using Sy
stem.Data;
using Sy
stem.Collections;
using Sy
stem.IO;
namespace YZControl
{
public class NewXmlControl : Object
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
public NewXmlControl(string XmlFile,Boolean bOverWrite,string sRoot)
{
try
{
//如果覆盖模式,则强行创建
一个xml文档
if (bOverWrite)
{
objXmlDoc.AppendChild(objXmlDoc.CreateXmlDecl
aration("1.0","utf-8",null));//设置xml的版本,格式信息
objXmlDoc.AppendChild(objXmlDoc.CreateElement("",sRoot,""));//创建根元素
objXmlDoc.Save(XmlFile);//保存
}
else //否则,
检查文件是否存在,不存在则创建
{
if (!(File.Exists(XmlFile)))
{
objXmlDoc.AppendChild(objXmlDoc.CreateXmlDecl
aration("1.0",null));
objXmlDoc.AppendChild(objXmlDoc.CreateElement("",""));
objXmlDoc.Save(XmlFile);
}
}
objXmlDoc.Load(XmlFile);
}
catch (Sy
stem.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
/// <summary>
/// 根据xPath值,返回xPath下的所有下级子结节到
一个DataView
/// </summary>
/// <p
aram name="XmlPathNode">xPath值</p
aram>
/// <returns>有数据则返回DataView,否则返回null</returns>
public DataView GetData(string XmlPathNode)
{
//查找数据。返回
一个DataView
DataSet ds = new DataSet();
try
{
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.
readxml(read);
return ds.Tables[0].
defaultview;
}
catch
{
//throw;
return null;
}
}
/// <summary>
/// 更新节点
内容
/// </summary>
/// <p
aram name="xmlPathNode"></p
aram>
/// <p
aram name="content"></p
aram>
public void UpdateNode(string xmlPathNode,string content)
{
objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
}
/// <summary>
/// 更新节点的某个
属性
/// </summary>
/// <p
aram name="xmlPathNode">要操作的节点</p
aram>
/// <p
aram name="AttribName">
属性名</p
aram>
/// <p
aram name="AttribValue">
属性值</p
aram>
public void UpdateNode(string xmlPathNode,string AttribName,string AttribValue)
{
((XmlElement)(objXmlDoc.SelectSingleNode(xmlPathNode))).SetAttribute(AttribName,AttribValue);
}
/// <summary>
///
修改节点(同步更新
内容和
属性)
/// </summary>
/// <p
aram name="xmlPathNode">要操作节点的xpath语句</p
aram>
/// <p
aram name="arrAttribName">
属性名称字符串数组</p
aram>
/// <p
aram name="arrAttribContent">
属性内容字符串数组</p
aram>
/// <p
aram name="content">节点
内容</p
aram>
public void UpdateNode(string xmlPathNode,string[] arrAttribName,string[] arrAttribContent,string content)
{
XmlNode xn = objXmlDoc.SelectSingleNode(xmlPathNode);
if (xn != null)
{
xn.InnerText = content;
xn.Attributes.RemoveAll();
for (int i = 0; i <= arrAttribName.GetUpperBound(0); i++)
{
((XmlElement)(xn)).SetAttribute(arrAttribName[i],arrAttribContent[i]);
}
}
}
/// <summary>
/// 移除选定节点集的所有
属性
/// </summary>
/// <p
aram name="xmlPathNode"></p
aram>
public void RemoveAllAttribute(string xmlPathNode)
{
XmlNodeList xnl = objXmlDoc.SelectNodes(xmlPathNode);
foreach (XmlNode xn in xnl)
{
xn.Attributes.RemoveAll();
}
}
public void DeleteNode(string Node)
{
//刪除
一个节点。
try
{
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
catch
{
//throw;
return;
}
}
public void InsertNodeWithChild(string mainNode,string ChildNode,string Element,string Content)
{
//插入一节点和此节点的一子节点。
XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);//插入节点
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);//插入子节点
}
/// <summary>
/// 插入
一个节点,带
一个Attribute和innerText
/// </summary>
/// <p
aram name="mainNode"></p
aram>
/// <p
aram name="Element">节点
名称</p
aram>
/// <p
aram name="Attrib">Attribute
名称</p
aram>
/// <p
aram name="AttribContent">Attribute值</p
aram>
/// <p
aram name="Content">innerText值</p
aram>
public void InsertNode(string mainNode,string Attrib,string AttribContent,string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 插入
一个节点,带
一个Attribute
/// </summary>
/// <p
aram name="mainNode"></p
aram>
/// <p
aram name="Element">节点
名称</p
aram>
/// <p
aram name="Attrib">Attribute
名称</p
aram>
/// <p
aram name="AttribContent">Attribute值</p
aram>
public void InsertNode(string mainNode,string AttribContent)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objNode.AppendChild(objElement);
}
/// <summary>
/// 插入
一个节点
/// </summary>
/// <p
aram name="mainNode"></p
aram>
/// <p
aram name="Element">节点
名称</p
aram>
public void InsertNode(string mainNode,string Element)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objNode.AppendChild(objElement);
}
//<summary>
//插入
一个节点,带多个
属性和
一个inner text
//</summary>
public void InsertNode(string mainNode,string elementName,string[] arrAttributeName,string[] arrAttributeContent,string elementContent)
{
try
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(elementName);
for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
{
objElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
}
objElement.InnerText = elementContent;
objNode.AppendChild(objElement);
}
catch
{
throw;
//string t = mainNode;
//;
}
}
///<summary>
///插入
一个节点,带多个
属性
///</summary>
public void InsertNode(string mainNode,string[] arrAttributeContent)
{
try
{
XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
XmlElement objElement = objXmlDoc.CreateElement(elementName);
for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
{
objElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
}
//objElement.InnerText = elementContent;
objNode.AppendChild(objElement);
}
catch
{
throw;
//string t = mainNode;
//;
}
}
/// <summary>
/// 插入子节点(带多个
属性)
/// </summary>
/// <p
aram name="parentNode">要插入的父节点</p
aram>
/// <p
aram name="elementName">插入的节点
名称</p
aram>
/// <p
aram name="arrAttributeName">
属性名称[数组]</p
aram>
/// <p
aram name="arrAttributeContent">
属性内容[数组]</p
aram>
/// <p
aram name="elementContent">节点
内容</p
aram>
public void AddChildNode(string parentNodePath,string elementContent)
{
try
{
XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
{
objChildElement.SetAttribute(arrAttributeName[i],arrAttributeContent[i]);
}
objChildElement.InnerText = elementContent;
parentNode.AppendChild(objChildElement);
}
catch
{
return;
}
}
/// <summary>
/// 插入子节点(将
内容以CData形式写入)
/// </summary>
/// <p
aram name="parentNode">要插入的父节点</p
aram>
/// <p
aram name="elementName">插入的节点
名称</p
aram>
/// <p
aram name="elementContent">节点
内容</p
aram>
public void AddChildNodeCData(string parentNodePath,string elementContent)
{
try
{
XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
//写入cData数据
XmlCDataSection xcds = objXmlDoc.CreateCDataSection(elementContent);
objChildElement.AppendChild(xcds);
parentNode.AppendChild(objChildElement);
}
catch
{
return;
}
}
/// <summary>
/// 插入子节点(仅
内容,不带
属性)
/// </summary>
/// <p
aram name="parentNode">要插入的父节点</p
aram>
/// <p
aram name="elementName">插入的节点
名称</p
aram>
/// <p
aram name="elementContent">节点
内容</p
aram>
public void AddChildNode(string parentNodePath,string elementContent)
{
try
{
XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
objChildElement.InnerText = elementContent;
parentNode.AppendChild(objChildElement);
}
catch
{
return;
}
}
/// <summary>
/// 根据xpath值查找节点
/// </summary>
/// <p
aram name="NodePath">要查找节点的xpath值</p
aram>
/// <returns>找到返回true,否则返回true</returns>
public bool FindNode(string NodePath)
{
try
{
if (objXmlDoc.SelectSingleNode(NodePath) != null)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
/// <summary>
///保存文档
/// </summary>
public void Save()
{
//保存文档。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (Sy
stem.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}
}
调用方法
NewXmlControl xc = new NewXmlControl(Server.MapPath("~/
RSS.xml"),true,"
RSS");
xc.UpdateNode("//
RSS","version","2.0");
xc.InsertNode("//
RSS","channel");
xc.AddChildNode("/
RSS/channel","title",Shop.DAL.sp_netcon
fig.GetCon
figObj().webname);
// xc.AddChildNode("/
RSS/channel","slogan",Shop.DAL.sp_netcon
fig.GetCon
figObj().webname);
xc.AddChildNode("/
RSS/channel","link",Shop.DAL.sp_netcon
fig.GetCon
figObj().weburl);
xc.AddChildNode("/
RSS/channel","language","zh-cn");
xc.AddChildNode("/
RSS/channel","description",Shop.DAL.sp_netcon
fig.GetCon
figObj().
Metatwo);
// xc.AddChildNode("/
RSS/channel","
copyright",Shop.DAL.sp_netcon
fig.GetCon
figObj().
copyright);
xc.AddChildNode("/
RSS/channel","author","generator","
RSS Generator By Taoxian");
DataSet ds = DbHelper
sql.Query("select top 20 pro_ID,pro_Name,pro_CreateTime,pro_Content from sp_product where pro_SaleType=1 and pro_Stock>0 and pro_Audit=1 order by pro_ID desc");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
int j = i + 1;
xc.InsertNode("/
RSS/channel","item");
xc.AddChildNode("/
RSS/channel/item[" + j.ToString() + "]",ds.Tables[0].Rows[i]["pro_Name"].ToString());
xc.AddChildNode("/
RSS/channel/item[" + j.ToString() + "]",Shop.DAL.sp_netcon
fig.GetCon
figObj().weburl + "/Product/ProductInfo_" + ds.Tables[0].Rows[i]["pro_ID"].ToString() + ".html");
xc.AddChildNode("/
RSS/channel/item[" + j.ToString() + "]","pubDate",Convert.ToDateTime(ds.Tables[0].Rows[i]["pro_CreateTime"].ToString()).GetDateTimeFormats('r')[0].ToString());
xc.AddChildNode("/
RSS/channel/item[" + j.ToString() + "]",Shop.DAL.sp_netcon
fig.GetCon
figObj().webname);
xc.AddChildNodeCData("/
RSS/channel/item[" + j.ToString() + "]",ds.Tables[0].Rows[i]["pro_Content"].ToString());
}
ds.
dispose();
xc.Save();
YZControl.staticFunction.FinalMessage("
生成RSS成功!","html.aspx",2);