前面说到SQL如何操作XML,现在说说程序里如何操作XML。
#region 变量的定义
private XmlDocument _document = new XmlDocument();
private string _xmlPath;//文件路径
private string _nodePath;//节点路径
///
/// 操作的XML文档路径=文件路径+文件全名
///
public string XmlPath
{
get { return _xmlPath; }
set { _xmlPath = value; }
}
///
/// XML文档中的节点路径"//根节点/子节点/子节点……"
///
public string NodePath
{
get { return _nodePath; }
set { _nodePath = value; }
}
#endregion
#region 构造函数
///
/// 构造函数
///
/// XML文档路径+xml文件全名
public XmlManager(string xmlPath, string nodePath)
{
this._xmlPath = xmlPath;
if (nodePath != null && nodePath.Trim() != "")
{
this._nodePath = nodePath;
}
this._document.Load(xmlPath);
}
public XmlManager(string xmlStream)
{
try
{
System.IO.StringReader readStram = new System.IO.StringReader(xmlStream);
this._document.Load(readStram);
}
catch (Exception ex)
{
throw (new Exception("XML代码错误信息:" + ex.Message));
}
}
#endregion
#region 各种操作方法
///
/// 读出指定路径XML文档的全部内容
///
/// XML文档的全部内容
public string Out()
{
return this._document.OuterXml;
}
#region 获取节点
/// 获取XML中节点的内容
///
/// 第i段数据
/// 第j个属性
/// 用户指定节点路径
///
public string GetNode(int i, int j, string path)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(path);
return nodeList.Item(i - 1).ChildNodes.Item(j - 1).InnerText;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 获取XML中节点的内容
///
/// 第i段数据
/// 第j个属性
///
public string GetNode(int i, int j)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(_nodePath);
return nodeList.Item(i - 1).ChildNodes.Item(j - 1).InnerText;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 获取XML中节点的内容
///
/// 第i段数据
/// 节点名
/// 用户指定路径
///
public string GetNode(int i, string nodePath, string nodeName)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(nodePath);
for (int j = 0; j <= nodeList.Item(i - 1).ChildNodes.Count; j++)
{
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
return nodeList.Item(i - 1).ChildNodes.Item(j).InnerText;
}
return "nofind";
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 获取XML中节点的内容
///
/// 第i段数据
/// 节点名
///
public string GetNode(int i, string nodeName)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(_nodePath);
for (int j = 0; j <= nodeList.Item(i - 1).ChildNodes.Count; j++)
{
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
return nodeList.Item(i - 1).ChildNodes.Item(j).InnerText;
}
return "nofind";
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
#endregion
#region 获取属性
/// <summary>
/// 获取节点上属性值
/// </summary>
/// <param name="i">第几个节点</param>
/// <param name="j">第几段数据</param>
/// <param name="j">第几段属性</param>
/// <param name="path">节点路径</param>
/// <returns></returns>
public string GetAttribute(int i, int j, int y, string path)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(path);
return nodeList.Item(i - 1).ChildNodes.Item(j - 1).Attributes[y - 1].Value;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
/// <summary>
/// 遍历XML获取信息
/// </summary>
/// <param name="path">XML路径</param>
/// <returns></returns>
public string GetRespone(string path)
{
var xd = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath(path));
string e = null;
foreach (var item in xd.Root.Elements())
{
if (item.Attribute("succeed").Value != "0")
{
e+=item.Attribute("dsc").Value + "<br>";
}
}
return e;
}
#endregion
#region 获取节点数
///
/// 返回符合指定名称的节点数
///
/// 节点名
/// 节点数
public int Count(string nodeName)
{
try
{
XmlNodeList nodeList = this._document.GetElementsByTagName(nodeName);
return nodeList.Count;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 使用设置的节点路径返回符合指定名称的节点数
///
/// 节点数
public int Count()
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(_nodePath);
return nodeList.Count;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 返回指符合指定名称的节点集中第i个集合的子节点数
///
///
///
public int CountChilds(int i, string nodeName)
{
try
{
XmlNodeList nodeList = this._document.GetElementsByTagName(nodeName);
if (nodeList.Count > 0)
{
return nodeList.Item(i - 1).ChildNodes.Count;
}
else
{
return 0;
}
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 返回指符合指定名称的节点的子节点数
///
///
///
public int CountChilds(string nodeName)
{
try
{
int counts = 0;
XmlNodeList nodeList = this._document.GetElementsByTagName(nodeName);
if (nodeList.Count > 0)
{
for (int i = 0; i < nodeList.Count; i++)
{
counts += nodeList.Item(i).ChildNodes.Count;
}
return counts;
}
else
{
return 0;
}
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
#endregion
#region 修改节点值
///
/// 修改指定节点的值
///
/// 节点名称
/// 新值
/// 返回更改的节点数
public int SetNode(string nodeName, string newValue)
{
try
{
XmlNodeList nodeList = this._document.GetElementsByTagName(nodeName);
foreach (XmlNode node in nodeList)
{
node.InnerText = newValue;
}
this._document.Save(_xmlPath);
return nodeList.Count;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 修改指定节点的值
///
/// 第i段数据
/// 节点路径
/// 节点名称
/// 新值
public void SetNode(int i, string nodePath, string nodeName, string newValue)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(nodePath);
if (nodeList.Count > 0 && nodeList.Count <= i)
{
for (int j = 0; j < nodeList.Item(i - 1).ChildNodes.Count; j++)
{
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
{
nodeList.Item(i - 1).ChildNodes.Item(j).InnerText = newValue;
this._document.Save(_xmlPath);
}
}
}
else
{
throw (new Exception("并无此子集"));
}
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 修改指定节点的值,使用设置的节点路径
/// 第i段数据
/// 节点名称
/// 新值
public void SetNode(int i, string nodeName, string newValue)
{
try
{
XmlNodeList nodeList = this._document.SelectNodes(_nodePath);
if (nodeList.Count > 0 && nodeList.Count <= i)
{
for (int j = 0; j < nodeList.Item(i - 1).ChildNodes.Count; j++)
{
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
{
nodeList.Item(i - 1).ChildNodes.Item(j).InnerText = newValue;
this._document.Save(_xmlPath);
}
}
}
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
#endregion
#region 插入节点
///
/// 为指定的节点(集插)入一个子节点
///
/// 父节点
/// 子节点名
/// 子节点值
public void InsetNode(string parentName, string nodeName, string nodeVale)
{
XmlNodeList nodeList = this._document.GetElementsByTagName(parentName);
for (int i = 0; i < nodeList.Count; i++)
{
XmlElement newElement = _document.CreateElement(nodeName);
//newElement.SetAttribute(nodeName,nodeVale);//会插入如的节点
nodeList.Item(i).AppendChild(newElement);//002
newElement.InnerText = nodeVale;
}
this._document.Save(this._xmlPath);
}
///
/// 为指定的节点(集)插入一个子节点
///
/// 父节点
/// 子节点名
/// 子节点值
public void InsetNode(int i, string parentName, string nodeName, string nodeValue)
{
try
{
XmlNodeList nodeList = this._document.GetElementsByTagName(parentName);
XmlElement newElement = _document.CreateElement(nodeName);
//newElement.SetAttribute(nodeName, nodeValue);
if (nodeList.Count > 0 && nodeList.Count <= i)
{
nodeList.Item(i - 1).AppendChild(newElement);
newElement.InnerText = nodeValue;
this._document.Save(this._xmlPath);
}
else
{
throw (new Exception("无此节点"));
}
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
///
/// 插入根节点(第二级根节点)
///
/// 节点名
/// 子节点名
/// 子节点值
public void InsertRootNode(string rootName, string[] nodeName, string[] nodeValue)
{
XmlElement root = _document.DocumentElement;
XmlElement newRoot = _document.CreateElement(rootName);
root.AppendChild(newRoot);
for (int i = 0; i < nodeName.Length; i++)
{
XmlElement newChild = _document.CreateElement(nodeName[i]);
newRoot.AppendChild(newChild);
newChild.InnerText = nodeValue[i];
}
this._document.Save(this._xmlPath);
}
//public string geT(string name)
//{
// XmlNodeList n = _document.GetElementsByTagName(name);
// XmlElement nn = _document.
// return nn[0].Attributes.ToString();
//}
#endregion
public void DeleteNote(string parentName, string noteName)
{
XmlNodeList nodeList = _document.GetElementsByTagName(parentName);
foreach (XmlNode node in nodeList)
{
foreach (XmlNode nodechild in node.ChildNodes)
if (nodechild.Name == noteName)
{
node.RemoveChild(nodechild);
}
}
this._document.Save(this._xmlPath);
}
public void DeleteNote(string parmentName)
{
XmlNodeList nodeList = this._document.GetElementsByTagName(parmentName);
foreach (XmlNode node in nodeList)
node.RemoveAll();
this._document.Save(_xmlPath);
}
public void DeleteAll()
{
XmlElement element = this._document.DocumentElement;
element.RemoveAll();
this._document.Save(this._xmlPath);
}
public Boolean Save(string xmlName, string rootElement)
{
try
{
string savePath = System.IO.Directory.GetCurrentDirectory() + "\\" + xmlName;
XmlDocument document = new XmlDocument();
XmlElement element = document.CreateElement(rootElement);
document.AppendChild(element);
document.Save(savePath);
return true;
}
catch (XmlException xe)
{
throw (new Exception(xe.Message));
}
}
#endregion