• xml帮助类


    自己今天刚刚写的一个xml的帮助类,有了它以后操作xml应该就会减轻一点负担吧。还没写好,不过写记下来。以后用到再补充吧,暂时够用了先。

    代码
    /// <summary>
    /// xml帮助类
    /// </summary>
    public class XmlHelper
    {
    #region

    /// <summary>
    /// 新建一个带有根节点的xml
    /// </summary>
    /// <param name="rootName">根节点名字</param>
    /// <returns></returns>
    public static XmlDocument CreateXml(string rootName)
    {
    XmlDocument xml
    = new XmlDocument();

    //写入声明
    XmlNode node = xml.CreateNode(XmlNodeType.XmlDeclaration, "", "");
    xml.AppendChild(node);

    //增加根节点
    XmlElement rootElement = xml.CreateElement(rootName);
    xml.AppendChild(rootElement);

    return xml;
    }


    /// <summary>
    /// 添加一个父节点到根节点末尾
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="parentNodeName">父节点名字</param>
    /// <returns></returns>
    public static void AddParentNode(XmlDocument xml, string parentNodeName)
    {
    XmlElement parentElement
    = xml.CreateElement(parentNodeName);
    xml.DocumentElement.AppendChild(parentElement);
    }


    /// <summary>
    /// 添加一个新的子节点(包括属性信息)到指定的节点中
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="currentPath">新节点的父节点xPath路径</param>
    /// <param name="elementName">新节点的名字</param>
    /// <param name="attrib">新节点的属性名字</param>
    /// <param name="attribContent">新节点的属性值</param>
    /// <param name="content">新节点值</param>
    /// <returns></returns>
    public static bool AddElement(XmlDocument xml, string currentNodePath, string elementName
    ,
    string attrib, string attribContent, string content)
    {
    bool flag = false;
    XmlNode currentNode
    = xml.SelectSingleNode(currentNodePath);
    if (currentNode != null)
    {
    XmlElement newElement
    = xml.CreateElement(elementName);
    newElement.SetAttribute(attrib, attribContent);
    newElement.InnerText
    = content;
    currentNode.AppendChild(newElement);
    flag
    = true;
    }
    return flag;
    }

    /// <summary>
    /// 添加一个新的子节点(不包括属性信息)到指定的节点中
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="currentNodePath">新节点的父节点xPath路径</param>
    /// <param name="elementName">新节点名字</param>
    /// <param name="content">新节点值</param>
    /// <returns></returns>
    public static bool AddElement(XmlDocument xml, string currentNodePath, string elementName
    ,
    string content)
    {
    bool flag = false;
    XmlNode currentNode
    = xml.SelectSingleNode(currentNodePath);
    if (currentNode != null)
    {
    XmlElement newElement
    = xml.CreateElement(elementName);
    newElement.InnerText
    = content;
    currentNode.AppendChild(newElement);
    flag
    = true;
    }
    return flag;
    }

    /// <summary>
    /// 向某一节点添加属性
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="currentNodePath">属性的节点的xPath</param>
    /// <param name="attributeName">属性名</param>
    /// <param name="attributeValue">属性值</param>
    /// <returns></returns>
    public static bool AddAttribute(XmlDocument xml,string currentNodePath,string attributeName
    ,
    string attributeValue)
    {
    bool flag = false;
    XmlNode currentNode
    = xml.SelectSingleNode(currentNodePath);
    if (currentNodePath != null)
    {
    XmlAttribute attribute
    = xml.CreateAttribute(attributeName); //创建一个新属性
    currentNode.Attributes.Append(attribute);
    XmlElement currentElement
    = currentNode as XmlElement;
    currentElement.SetAttribute(attributeName, attributeValue);
    flag
    = true;
    }
    return flag;
    }

    #endregion

    #region

    /// <summary>
    /// 查找指定节点名的节点列表
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="nodeName">节点名称</param>
    /// <returns>符合要求的节点列表</returns>
    public static XmlNodeList QueryNodeList(XmlDocument xml, string nodeName)
    {
    XmlNodeList nodeList
    = xml.GetElementsByTagName(nodeName);
    if (nodeList.Count != 0)
    {
    return nodeList;
    }
    return null;
    }

    /// <summary>
    /// 查找指定节点的指定属性值
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="nodePath">节点的xPath路径,必须是独一无二的。如果有多个则只返回第一个</param>
    /// <param name="attributeName">要查询的属性</param>
    /// <returns></returns>
    public static string QueryAttribute(XmlDocument xml, string nodePath, string attributeName)
    {
    string res = null;
    XmlNode currentNode
    = xml.SelectSingleNode(nodePath);
    if (currentNode != null)
    {
    XmlAttributeCollection xac
    = currentNode.Attributes; //当前节点的属性集合
    for (int i = 0; i < xac.Count; i++)
    {
    if (xac[i].Name == attributeName)
    {
    res
    = xac[i].Value;
    break;
    }
    }
    }
    return res;
    }

    /// <summary>
    /// 查找指定节点的值
    /// </summary>
    /// <param name="xml">xml文档</param>
    /// <param name="nodepath">节点的xPath路径,必须是独一无二的。如果有多个则只返回第一个</param>
    /// <returns></returns>
    public static string QueryNode(XmlDocument xml, string nodepath)
    {
    string res = null;
    XmlNode currentNode
    = xml.SelectSingleNode(nodepath);
    if (currentNode != null)
    {
    res
    = currentNode.InnerText;
    }
    return res;
    }

    #endregion

    #region

    public static void Save(XmlDocument xml, string path)
    {
    xml.Save(path);
    }

    #endregion

    #region

    /// <summary>
    /// 将xml转换为dataset
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    public static DataSet XmlToDataset(XmlDocument xml)
    {
    DataSet dataSet
    = new DataSet();
    //TODO: dataset
    return dataSet;
    }

    #endregion
    }
  • 相关阅读:
    [Angular 9] Built-in template syntax $any
    [Angular 9] Improved Dependency Injection with the new providedIn scopes 'any' and 'platform'
    [Angular 9 Unit testing] Stronger typing for dependency injection in tests
    [Angular] Preserve the current route’s query parameters when navigating with the Angular Router
    [Angular] Do relative routing inside component
    [Typescript] Make your optional fields required in TypeScript
    [Typescript] Exclude Properties from a Type in TypeScript
    [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
    [Debug] Set and remove DOM breakpoints
    【职业素养】4种让你显得没教养的做法
  • 原文地址:https://www.cnblogs.com/qianlifeng/p/1721486.html
Copyright © 2020-2023  润新知