xml处理的一个常用类

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;

namespace XmlProcess
{
 /// <summary>
 /// XML文件操作
 /// </summary>
 public sealed class XmlProcess
 {
  #region 私有变量
  /// <summary>
  /// 待操作的xml文件路径
  /// </summary>
  private string xmlFileName = string.Empty;
  /// <summary>
  /// xml的根元素
  /// </summary>
  private string rootNodeName = string.Empty;
  /// <summary>
  /// xml文档对象
  /// </summary>
  private XmlDocument XmlDoc = new XmlDocument();

  #endregion

  #region 公共变量

  

  #endregion

  #region 构造函数

  /// <summary>
  /// 指明所关联的xml文件的构造函数
  /// </summary>
  /// <param name="fileFullName">XML文件的全路径名(包括文件名)</param>
  /// <remarks></remarks>
  public XmlProcess(string fileFullName)
  {
   xmlFileName = fileFullName;
   try
   {
    XmlDoc.Load(fileFullName);
    rootNodeName += XmlDoc.DocumentElement.Name;
   }
   catch
   {
    throw new XmlException("XMLファイルを見つけてないです、確認してください。");
   }
  }

  #endregion

  #region 属性
  
  /// <summary>
  /// 关联的xml文件名
  /// </summary>
  public string XmlFileName
  {
   get
   {
    return xmlFileName;
   }
  }

  /// <summary>
  /// 根元素名
  /// </summary>
  public string RootNodeName
  {
   get
   {
    return rootNodeName;
   }
  }

  #endregion

  #region 公共方法

  #region AddNode

  /// <summary>
  /// 添加新节点
  /// </summary>
  /// <param name="parentNode">父节点名</param>
  /// <param name="nodeName">新节点名</param>
  /// <param name="nodeValue">新节点内容</param>
  /// <remarks>
  /// </remarks>  
  public int AddNode(string parentNode, string nodeName, string nodeValue)
  {
   try
   {
    XmlNode Root = XmlDoc.DocumentElement.SelectSingleNode(parentNode);
    if(Root != null)
    {
     XmlElement Element = XmlDoc.CreateElement(nodeName);    
     Element.InnerText = nodeValue.ToString();
     Root.AppendChild(Element);
     XmlDoc.Save(xmlFileName);
     return 0;//正常
    }
    else
    {
     return 1;//指定的节点不存在
    }
   }
   catch(XPathException ex)
   {    
    return 2;//节点路径错误
   }
   catch(InvalidOperationException ex)
   {
    return 5;//插入节点与父节点冲突
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;//XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  #endregion
  
  #region AddAttribute

  /// <summary>
  /// 添加属性
  /// </summary>
  /// <param name="xPath">待添加属性的位置</param>
  /// <param name="attrName">属性名</param>
  /// <param name="attrValue">属性内容</param>
  public int AddAttribute(string xPath, string attrName, string attrValue)
  { 
   try
   {
    //找到目标节点
    XmlNode Root = XmlDoc.DocumentElement.SelectSingleNode(xPath);
    if(Root != null)
    {
     //创建属性节点并赋值
     XmlAttribute Attr = XmlDoc.CreateAttribute(attrName);
     Attr.InnerText = attrValue;
     //目标节点上追加属性
     Root.Attributes.Append(Attr);
     //保存XML文件
     XmlDoc.Save(xmlFileName);
     return 0;
    }
    else
    {
     return 1;//指定的节点不存在
    }
   }
   catch(XPathException ex)
   {
    return 2;//节点路径错误
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  #endregion

  #region AlterAttribute

  /// <summary>
  /// 修改属性内容,如果指定属性不存在则不修改
  /// </summary>
  /// <param name="xPath">待修改属性的节点</param>
  /// <param name="attrName">待修改的属性名</param>
  /// <param name="attrValue">新属性值</param>
  public int AlterAttribute(string xPath, string attrName, string attrValue)
  {
   return AlterAttribute(xPath, attrName, attrValue, false);
  }

  /// <summary>
  /// 修改属性内容,如果指定属性不存在则可以选择该属性是否追加
  /// </summary>
  /// <param name="xPath">待修改属性的节点</param>
  /// <param name="attrName">待修改的属性名</param>
  /// <param name="attrValue">新属性值</param>
  /// <param name="addOrNot">true追加,fasle不追加</param>
  public int AlterAttribute(string xPath, string attrName, string attrValue, bool addOrNot)
  {  
   try
   {
    if(addOrNot)//属性不存在时追加
    {
     //找到目标节点如果存在则进行属性追加.成功返回0,不成功返回大于0的值.
     XmlNode Root = XmlDoc.DocumentElement.SelectSingleNode(xPath);     
     if(Root != null)
     {
      //创建属性节点并赋值
      XmlAttribute Attr = XmlDoc.CreateAttribute(attrName);
      Attr.InnerText = attrValue;
      //追加属性
      Root.Attributes.Append(Attr);
      //保存XML文件
      XmlDoc.Save(xmlFileName);
      return 0;
     }
     else
     {
      return 1;//指定的节点不存在
     }     
    }
    else//属性不存在时不追加
    {
     //查找属性如果存在则进行修改,不存在则不做任何处理.
     XmlAttribute Attr = XmlDoc.DocumentElement.SelectSingleNode(xPath).Attributes[attrName];
     if(Attr != null)
     {
      Attr.InnerText = attrValue;
      XmlDoc.Save(xmlFileName);      
     }
     return 0;
    }    
   }
   catch(XPathException ex)
   {
    return 2;//节点路径错误
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  #endregion
  
  #region DelNode

  /// <summary>
  /// 删除节点
  /// </summary>
  /// <param name="xPath">指定节点</param>
  /// <param name="selfInclude">是否删除指定节点的标签,true表示删除</param>
  public int DelNode(string xPath, bool selfInclude)
  {  
   try
   {
    XmlNode TargetNode = XmlDoc.DocumentElement.SelectSingleNode(xPath);
    if (TargetNode != null)
    {
     if(selfInclude)//整个节点全部删除
     {
      XmlNode ParentNode = TargetNode.ParentNode;
      ParentNode.RemoveChild(TargetNode);
     }
     else//只删除节点内容
     {
      TargetNode.RemoveAll();
     }
     //保存修改
     XmlDoc.Save(xmlFileName);
     return 0;//正确返回
    }
    else
    {
     return 1;
    }
   }
   catch(XPathException ex)
   {
    return 2;//节点路径错误
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  /// <summary>
  /// 指定某一ID属性值的节点删除.
  /// </summary>
  /// <param name="xPath">指定节点</param>
  /// <param name="attrNameValue">属性值,</param>
  /// <param name="selfInclude">是否删除指定节点的标签,true表示删除</param>
  public int DelNode(string xPath, string attrValue, bool selfInclude)
  {
   return DelNode(xPath, "ID", attrValue, selfInclude);
  }

  /// <summary>
  /// 指定某一ID属性值的节点删除.
  /// </summary>
  /// <param name="xPath">指定节点</param>
  /// <param name="attrName">属性名</param>
  /// <param name="attrValue">属性值</param>
  /// <param name="selfInclude">是否删除指定节点的标签,true表示删除</param>
  /// <returns>成功返回0,失败返回大于0的值</returns>
  public int DelNode(string xPath, string attrName, string attrValue, bool selfInclude)
  {
   //格式化节点路径最终xPath = xPath[@attrName='attrValue']
   xPath = FormatXPath(xPath, attrName, attrValue);
   return DelNode(xPath, selfInclude);
  }

  #endregion

  #region DelNodes

  /// <summary>
  /// 一次删除多个节点
  /// </summary>
  /// <param name="xPath">删除节点的路径</param>
  /// <param name="selfInclude">是否连同标签一起删除.true表示删除.</param>
  public int DelNodes(string xPath, bool selfInclude)
  {
   try
   {
    //取得待删除节点集
    XmlNodeList TargetNodes = XmlDoc.DocumentElement.SelectNodes(xPath);
    if(TargetNodes.Count >0)//存在满足条件的节点,则将这些节点删除
    {
     if(selfInclude)//删除整个节点
     {
      foreach(XmlNode node in TargetNodes)
      {
       node.ParentNode.RemoveChild(node);
      }      
     }
     else//删除节点内容
     {
      foreach(XmlNode node in TargetNodes)
      {
       node.RemoveAll();
      }     
     }
     //保存修改
     XmlDoc.Save(xmlFileName);
     return 0;//正常返回
    }
    else
    {
     return 1;//待删除节点不存在
    }
   }
   catch(XPathException ex)
   {
    return 2;//节点路径错误
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  /// <summary>
  /// 指定属性的多个节点删除
  /// </summary>
  /// <param name="xPath">删除节点的路径</param>
  /// <param name="attrNameValue">属性值</param>
  /// <param name="selfInclude">是否连同标签一起删除.true表示删除.</param>
  public int DelNodes(string xPath, string attrValue, bool selfInclude)
  {
   return DelNodes(xPath, "ID", attrValue, selfInclude);
  }

  /// <summary>
  /// 指定属性的多个节点删除
  /// </summary>
  /// <param name="xPath">删除节点的路径</param>
  /// <param name="attrName">属性名</param>
  /// <param name="attrValue">属性</param>
  /// <param name="selfInclude">是否连同标签一起删除.true表示删除.</param>
  public int DelNodes(string xPath, string attrName, string attrValue, bool selfInclude)
  {
   //格式化节点路径最终xPath = "xPath[@attrName='attrValue']"
   xPath = FormatXPath(xPath, attrName, attrValue);
   return DelNodes(xPath, selfInclude);
  }

  #endregion

  #region DelAttribute

  /// <summary>
  /// 删除指定属性
  /// </summary>
  /// <param name="xPath">属性所在节点</param>
  /// <param name="attrName">待删除属性名</param>
  public int DelAttribute(string xPath, string attrName)
  {
   try
   {
    //取得目标节点,请将其转换为XmlElement类型
    XmlElement TargetElement = XmlDoc.DocumentElement.SelectSingleNode(xPath) as XmlElement;
    if(TargetElement != null)//元素存在
    {
     //删除属性
     TargetElement.RemoveAttribute(attrName);
     //保存修改
     XmlDoc.Save(xmlFileName);
     return 0;//正常返回
    }
    else
    {
     return 1;//没有指定的属性
    }
   }
   catch(XPathException ex)
   {
    return 2;//节点路径错误
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  #endregion

  #region DelAllAttributes

  /// <summary>
  /// 删除节点下的全部属性
  /// </summary>
  /// <param name="xPath">指定的节点</param>
  public int DelAllAttributes(string xPath)
  {
   try
   {
    //取得目标节点,请将其转换为XmlElement类型
    XmlElement TargetElement = XmlDoc.DocumentElement.SelectSingleNode(xPath) as XmlElement;
    if(TargetElement != null)//元素存在
    {
     //删除全部属性
     TargetElement.RemoveAllAttributes();
     //保存修改
     XmlDoc.Save(xmlFileName);
     return 0;//正常返回
    }
    else
    {
     return 1;//节点下没有属性
    }
   }
   catch(XPathException ex)
   {
    return 2;//节点路径错误
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  #endregion

  #region GetNodeValue

  /// <summary>
  /// 返回特定节点的内容
  /// </summary>  
  /// <param name="xPath">指定的节点</param>
  public string GetNodeValue(string xPath)
  {
   try
   {
    return XmlDoc.DocumentElement.SelectSingleNode(xPath).InnerText;
   }
   catch
   {
    return "";
   }
  }

  /// <summary>
  /// 返回包含ID属性是指定值的节点内容
  /// </summary>
  /// <param name="xPath">指定的节点</param>
  /// <param name="attrValue">属性值</param>
  public string GetNodeValue(string xPath, string attrValue)
  {
   return GetNodeValue(xPath, "ID", attrValue);
  }

  /// <summary>
  /// 返回包含指定属性名及指定属性值的节点内容
  /// </summary>
  /// <param name="xPath">指定的节点</param>
  /// <param name="attrName">属性名</param>
  /// <param name="attrValue">属性值</param>
  public string GetNodeValue(string xPath, string attrName, string attrValue)
  {
   //格式化节点路径 xPath = "xPath[@attrName='attrValue']"
   xPath = FormatXPath(xPath, attrName, attrValue);
   return GetNodeValue(xPath);   
  }

  #endregion
  
  #region GetNodesCount

  /// <summary>
  /// 指定位置下的节点的个数
  /// </summary>
  /// <param name="xPath">节点名.</param>
  public int GetNodesCount(string xPath)
  {
   try
   {
    return XmlDoc.DocumentElement.SelectNodes(xPath).Count;
   }
   catch
   {
    return -1;
   }
  }
  
  /// <summary>
  /// 指定位置下ID属性为指定值的节点的个数
  /// </summary>
  /// <param name="xPath">节点名.</param>
  /// <param name="attrValue">属性值</param>
  public int GetNodesCount(string xPath, string attrValue)
  {
   return GetNodesCount(xPath, "ID", attrValue);
  }

  /// <summary>
  /// 指定位置下ID属性为指定值的节点的个数
  /// </summary>
  /// <param name="xPath">节点名.</param>
  /// <param name="attrName">属性名</param>
  /// <param name="attrValue">属性值</param>
  /// <returns></returns>
  public int GetNodesCount(string xPath, string attrName, string attrValue)
  {
   //格式化节点路径 xPath = "xPath[@attrName='attrValue']"
   xPath = FormatXPath(xPath, attrName, attrValue);
   return GetNodesCount(xPath);
  }

  #endregion

  #region GetChildNodesCount

  /// <summary>
  /// 指定节点下的子节点数
  /// </summary>
  /// <param name="xPath">指定的节点</param>
  public int GetChildNodesCount(string xPath)
  {
   try
   {
    return XmlDoc.DocumentElement.SelectSingleNode(xPath).ChildNodes.Count;
   }
   catch
   {
    return -1;
   }
  }

  /// <summary>
  /// 指定节点下ID属性为指定值的子节点数
  /// </summary>
  /// <param name="xPath">指定的节点</param>
  /// <param name="attrValue">属性值</param>
  public int GetChildNodesCount(string xPath, string attrValue)
  {
   return GetChildNodesCount(xPath, "ID", attrValue);
  }

  /// <summary>
  /// 指定节点下指定属性为指定值的子节点数
  /// </summary>
  /// <param name="xPath">指定的节点</param>
  /// <param name="attrName">属性名</param>
  /// <param name="attrValue">属性值</param>
  public int GetChildNodesCount(string xPath, string attrName, string attrValue)
  {
   //格式化节点路径 xPath = "xPath[@attrName='attrValue']"
   xPath = FormatXPath(xPath, attrName, attrValue);
   return GetChildNodesCount(xPath);
  }

  #endregion

  #region SaveXmlFile

  /// <summary>
  /// 保存xml文件到指定位置
  /// </summary>
  /// <param name="filePath">文件新路径</param>
  public int SaveXmlFile(string filePath)
  {
   try
   {
    XmlDoc.Save(filePath);
    xmlFileName = filePath;
    return 0;
   }
   catch(XmlException ex)
   {
    return 4;//XML文件保存失败
   }
  }  

  #endregion

  #region GetDBSection

  /// <summary>
  /// 関数:データベース接続XML定義ファイルを読取する処理モジュール。
  /// </summary>
  /// <param name="sectionName">節点名</param>
  /// <param name="attrValue">節点ID値</param>
  /// <returns>文字列配列:1Server; 2Database; 3User; 4Password; 5Connecttimeout; 6Commandtimeout</returns>
  public string[] GetDBSection(string sectionName, string attrValue)
  {
   return GetDBSection(sectionName, "ID", attrValue);
  }
  
  public string[] GetDBSection(string sectionName, string attrName, string attrValue)
  {
   string[] DBSection = {"", "", "", "", "", ""};
   string NodePath = FormatXPath(sectionName, attrName, attrValue);
   try
   {
    XmlNodeList TargetNodes = XmlDoc.DocumentElement.SelectSingleNode(NodePath).ChildNodes;
    if(TargetNodes.Count > 5)
    {
     foreach(XmlNode childNode in TargetNodes)
     {
      if(childNode.Name == "Server")
      {
       DBSection[0] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Database")
      {
       DBSection[1] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "User")
      {
       DBSection[2] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Password")
      {
       DBSection[3] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Connecttimeout")
      {
       DBSection[4] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Commandtimeout")
      {
       DBSection[5] = childNode.InnerText.Trim();
      }
     }
    }
    return DBSection;
   }
   catch
   {
    return DBSection;
   }
  }

  #endregion

  #region ShowMessage

  /// <summary>
  /// 関数:メッセージXML定義ファイルを読取する処理モジュール
  /// </summary>
  /// <param name="sectionName">節点名</param>
  /// <param name="attrValue">節点ID値</param>
  /// <returns>メッセージボックスの本文、ボタン種類、アイコン、デフォルトボタン、タイトル</returns>  
  public DialogResult ShowMessage(string sectionName, string attrValue)
  {
   return ShowMessage(sectionName, "ID", attrValue);
  }
  
  public DialogResult ShowMessage(string sectionName, string attrName, string attrValue)
  {   
   string MsgTest = "";
   string MsgCaption = "";
   MessageBoxButtons MsgButton = MessageBoxButtons.OK;
   MessageBoxIcon MsgIcon = MessageBoxIcon.None;
   MessageBoxDefaultButton MsgDefault = MessageBoxDefaultButton.Button1;
   MessageBoxOptions MsgOption = MessageBoxOptions.DefaultDesktopOnly;
   string NodePath = FormatXPath(sectionName, attrName, attrValue);
   try
   {    
    XmlNodeList TargetNodes = XmlDoc.DocumentElement.SelectSingleNode(NodePath).ChildNodes;
    if(TargetNodes.Count > 4)
    {
     foreach(XmlNode childNode in TargetNodes)
     {
      if(childNode.Name == "Information")
      {
       MsgTest = childNode.InnerText;
      }
      else if(childNode.Name == "Caption")
      {
       MsgCaption = childNode.InnerText;
      }
      else if(childNode.Name == "BtnInfo")
      {
       MsgButton = GetMessageBoxButton(childNode.InnerText.Trim());
      }
      else if(childNode.Name == "IconInfo")
      {
       MsgIcon = GetMessageBoxIcon(childNode.InnerText.Trim());
      }
      else if(childNode.Name == "DefaultButton")
      {
       MsgDefault = GetMessageBoxDefaultButton(childNode.InnerText.Trim());
      }
      else if(childNode.Name == "Option")
      {
       MsgOption = GetMessageBoxOption(childNode.InnerText.Trim());
      }
     }
    }   
    return MessageBox.Show(MsgTest, MsgCaption, MsgButton, MsgIcon, MsgDefault, MsgOption);
   }
   catch
   {
    return MessageBox.Show(MsgTest, MsgCaption, MsgButton, MsgIcon, MsgDefault, MsgOption);
   }
  }

  #endregion

  #endregion

  #region public staticメソッド

  #region GreateXmlFile

  /// <summary>
  /// staticメソッド:XMLファイルの新規作成
  /// </summary>
  /// <param name="fileFullPath">XMLファイル保存パス</param>
  /// <param name="rootName">Root節点名</param>
  public static int CreateXmlFile(string fileFullPath, string encoding, string rootName)
  {
   try
   {
    XmlDocument Doc = new XmlDocument();
    XmlDeclaration Dec = Doc.CreateXmlDeclaration("1.0", encoding, null);
    Doc.AppendChild(Dec);
    XmlElement RootNode = Doc.CreateElement(rootName);
    Doc.AppendChild(RootNode);
    Doc.Save(fileFullPath);
    return 0;//正常返回
   }
   catch(XPathException ex)
   {    
    return 2;//节点路径错误
   }
   catch(InvalidOperationException ex)
   {
    return 5;//插入节点与父节点冲突
   }
   catch(ArgumentException ex)
   {
    return 3;//XML文件无法写入
   }
   catch(XmlException ex)
   {
    return 4;//XML文件保存失败
   }
   catch(Exception ex)
   {
    return 99;//不明错误
   }
  }

  public static int CreateXmlFile(string fileFullPath, string rootName)
  {
   return CreateXmlFile(fileFullPath, "UTF-8", rootName);
  }  

  public static int CreateXmlFile(string fileFullPath)
  {
   return CreateXmlFile(fileFullPath, "UTF-8", "Root");
  }

  #endregion

  #region GetDBSection

  /// <summary>
  /// staticメソッド:データベース接続XML定義ファイルを読取する処理モジュール
  /// </summary>
  /// <param name="XmlDoc">XMLファイル対象</param>
  /// <param name="sectionName">節点名</param>
  /// <param name="attrValue">節点ID値</param>
  /// <returns>文字列配列:1Server; 2Database; 3User; 4Password; 5Connecttimeout; 6Commandtimeout</returns>
  public static string[] GetDBSection(XmlDocument XmlDoc, string sectionName, string attrValue)
  {
   return GetDBSection(XmlDoc, sectionName, "ID", attrValue);
  }

  public static string[] GetDBSection(XmlDocument XmlDoc, string sectionName, string attrName, string attrValue)
  {
   string[] DBSection = {"", "", "", "", "", ""};
   string NodePath = FormatXPath(sectionName, attrName, attrValue);
   try
   {    
    XmlNodeList TargetNodes = XmlDoc.DocumentElement.SelectSingleNode(NodePath).ChildNodes;
    if(TargetNodes.Count > 5)
    {
     foreach(XmlNode childNode in TargetNodes)
     {
      if(childNode.Name == "Server")
      {
       DBSection[0] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Database")
      {
       DBSection[1] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "User")
      {
       DBSection[2] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Password")
      {
       DBSection[3] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Connecttimeout")
      {
       DBSection[4] = childNode.InnerText.Trim();
      }
      else if(childNode.Name == "Commandtimeout")
      {
       DBSection[5] = childNode.InnerText.Trim();
      }
     }
    }   
    return DBSection;
   }
   catch
   {
    return DBSection;
   }
  }

  #endregion

  #region ShowMessage

  /// <summary>
  /// staticメソッド:メッセージXML定義ファイルを読取する処理モジュール
  /// </summary>
  /// <param name="sectionName">節点名</param>
  /// <param name="attrValue">節点ID値</param>
  /// <returns>メッセージボックスの本文、ボタン種類、アイコン、デフォルトボタン、タイトル</returns>
  
  public static DialogResult ShowMessage(XmlDocument XmlDoc, string sectionName, string attrValue)
  {
   return ShowMessage(XmlDoc, sectionName, "ID", attrValue);
  }

  public static DialogResult ShowMessage(XmlDocument XmlDoc, string sectionName, string attrName, string attrValue)
  {   
   string MsgTest = "";
   string MsgCaption = "";
   MessageBoxButtons MsgButton = MessageBoxButtons.OK;
   MessageBoxIcon MsgIcon = MessageBoxIcon.None;
   MessageBoxDefaultButton MsgDefault = MessageBoxDefaultButton.Button1;
   MessageBoxOptions MsgOption = MessageBoxOptions.DefaultDesktopOnly;
   string NodePath = FormatXPath(sectionName, attrName, attrValue);
   try
   {    
    XmlNodeList TargetNodes = XmlDoc.DocumentElement.SelectSingleNode(NodePath).ChildNodes;
    if(TargetNodes.Count > 4)
    {
     foreach(XmlNode childNode in TargetNodes)
     {
      if(childNode.Name == "Information")
      {
       MsgTest = childNode.InnerText;
      }
      else if(childNode.Name == "Caption")
      {
       MsgCaption = childNode.InnerText;
      }
      else if(childNode.Name == "BtnInfo")
      {
       MsgButton = GetMessageBoxButton(childNode.InnerText.Trim());
      }
      else if(childNode.Name == "IconInfo")
      {
       MsgIcon = GetMessageBoxIcon(childNode.InnerText.Trim());
      }
      else if(childNode.Name == "DefaultButton")
      {
       MsgDefault = GetMessageBoxDefaultButton(childNode.InnerText.Trim());
      }
      else if(childNode.Name == "Option")
      {
       MsgOption = GetMessageBoxOption(childNode.InnerText.Trim());
      }
     }
    }
    return MessageBox.Show(MsgTest, MsgCaption, MsgButton, MsgIcon, MsgDefault, MsgOption);
   }
   catch
   {
    return MessageBox.Show(MsgTest, MsgCaption, MsgButton, MsgIcon, MsgDefault, MsgOption);
   }
  }

  #endregion

  #endregion

  #region  Private staticメソッド

  /// <summary>
  /// 格式化节点路径
  /// </summary>
  /// <param name="xPath"></param>
  /// <returns></returns>  
  private static string FormatXPath(string xPath, string attrValue)
  {
   return FormatXPath(xPath, "ID", attrValue);
  }

  private static string FormatXPath(string xPath, string attrName, string attrValue)
  {
   return xPath + "[@" + attrName.Trim() + "='" + attrValue.Trim() + "']";
  }
  /// <summary>
  /// Private staticメソッド
  /// </summary>
  /// <param name="ButtonName"></param>
  /// <returns></returns>
  private static MessageBoxButtons GetMessageBoxButton(string ButtonName)
  {
   string strName=ButtonName.Trim();
   strName=strName.ToLower();
   MessageBoxButtons Buttons;
   Buttons=MessageBoxButtons.OK;
   
   switch(strName.ToLower())
   {
    case "abortretryignore":
    {
     Buttons=MessageBoxButtons.AbortRetryIgnore;
     break;
    }//case AbortRetryIgnore
    case "okcancel":
    {
     Buttons=MessageBoxButtons.OKCancel;
     break;
    }//case OKCancel
    case "retrycancel":
    {
     Buttons=MessageBoxButtons.RetryCancel;
     break;
    }//case RetryCancel
    case "yesno":
    {
     Buttons=MessageBoxButtons.YesNo;
     break;
    }//case YesNo
    case "yesnocancel":
    {
     Buttons=MessageBoxButtons.YesNoCancel;
     break;
    }//case YesNoCancel
    default:
    {
     Buttons=MessageBoxButtons.OK;
     break;
    }//case OK
   }//switch Button
   
   return Buttons;
  }
  /// <summary>
  /// Private staticメソッド
  /// </summary>
  /// <param name="ButtonName"></param>
  /// <returns></returns>
  private static MessageBoxDefaultButton GetMessageBoxDefaultButton(string DefaultButtonName)
  {
   string strName=DefaultButtonName.Trim();
   strName=strName.ToLower();
   MessageBoxDefaultButton DefaultButton;
   DefaultButton=MessageBoxDefaultButton.Button1;
   
   switch(strName.ToLower())
   {
    case "button3":
    {
     DefaultButton=MessageBoxDefaultButton.Button3;
     break;
    }//case Button3
    case "button2":
    {
     DefaultButton=MessageBoxDefaultButton.Button2;
     break;
    }//case Button2
    default:
    {
     DefaultButton=MessageBoxDefaultButton.Button1;
     break;
    }//case default Button1
   }//switch DefaultButton
   
   return DefaultButton;
  }
  /// <summary>
  /// Private staticメソッド
  /// </summary>
  /// <param name="ButtonName"></param>
  /// <returns></returns>
  private static MessageBoxIcon GetMessageBoxIcon(string IconName)
  {
   string strName=IconName.Trim();
   strName=strName.ToLower();
   MessageBoxIcon Icon;
   Icon=MessageBoxIcon.None;
   
   switch(strName.ToLower())
   {
    case "asterisk":
     Icon=MessageBoxIcon.Asterisk;
     break;
    case "error":
     Icon=MessageBoxIcon.Error;
     break;
    case "exclamation":
     Icon=MessageBoxIcon.Exclamation;
     break;
    case "hand":
     Icon=MessageBoxIcon.Hand;
     break;
    case "information":
     Icon=MessageBoxIcon.Information;
     break;
    case "question":
     Icon=MessageBoxIcon.Question;
     break;
    case "stop":
     Icon=MessageBoxIcon.Stop;
     break;
    case "warning":
     Icon=MessageBoxIcon.Warning;
     break;
    default:
     Icon=MessageBoxIcon.None;
     break;
   }//switch Icon
   
   return Icon;
  }
  /// <summary>
  /// Private staticメソッド
  /// </summary>
  /// <param name="ButtonName"></param>
  /// <returns></returns>
  private static MessageBoxOptions GetMessageBoxOption(string OptionName)
  {
   string strName=OptionName.Trim();
   strName=strName.ToLower();
   MessageBoxOptions Option;
   Option=MessageBoxOptions.DefaultDesktopOnly;
   
   switch(strName.ToLower())
   {
    case "rightalign":
    {
     Option=MessageBoxOptions.RightAlign;
     break;
    }//case RightAlign
    case "rtlreading":
    {
     Option=MessageBoxOptions.RtlReading;
     break;
    }//case RtlReading
    case "servicenotification":
    {
     Option=MessageBoxOptions.ServiceNotification;
     break;
    }//case ServiceNotification
    default:
    {
     Option=MessageBoxOptions.DefaultDesktopOnly;
     break;
    }//case default DefaultDesktopOnly
          
   }//switch Option
   
   return Option;
  }

  #endregion

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值