zoukankan      html  css  js  c++  java
  • xml操作

     string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "temp\\StatisticsDetail.config";

    /// <summary>
    /// 添加父节点
    /// </summary>
    /// <param name="content"></param>

    public void InsertParentNode(string nodeName, string count)

    {
    XmlElement root = this.LoadXmlConfig();
    XmlElement xmlElement = root.OwnerDocument.CreateElement(nodeName);
    xmlElement.SetAttribute("value", count);
    root.AppendChild(xmlElement);
    WriteToConfig(root.OuterXml);
    }

    /// <summary>
    /// 向配置文件中添加内容
    /// </summary>
    /// <param name="content"></param>
    public void WriteToConfig(string content)
    {
    try
    {
    using (StreamWriter sw = new StreamWriter(path))
    {
    sw.Write(content);
    }
    }
    catch (Exception e)
    {
    throw;
    }
    }

    /// <summary>
    /// load xml文件
    /// </summary>
    /// <returns></returns>
    private XmlElement LoadXmlConfig()
    {
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(LoadFileContent());
    return xml.DocumentElement;
    }

    /// <summary>
    /// 加载某一文本文件内容 将统计信息暂时保存在临时文件中 该文件内容要在一天统计完后添加到数据库中
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public string LoadFileContent()
    {
    string fileContent = null;
    //判断文件是否存在
    if (!File.Exists(path))
    {
    WriteToConfig("<configuration></configuration>");
    }
    fileContent = File.ReadAllText(path);
    return fileContent;
    }

    /// <summary>
    /// 添加子节点 如:日请求时要记录每个小时的请求量 也就是每隔一个小时要添加一个子节点
    /// </summary>
    /// <param name="datelength">子节点名</param>
    /// <param name="count">节点值</param>
    /// <param name="parentName">父节点的节点名</param>
    public void InsertChildNode(string datelenth, string count, string parentName)
    {
    XmlElement root = this.LoadXmlConfig();
    XmlNode webNode = root.SelectSingleNode(parentName);
    if (webNode != null)
    {
    XmlElement xmlElement = webNode.OwnerDocument.CreateElement(datelenth);
    xmlElement.SetAttribute("value", count);
    webNode.AppendChild(xmlElement);
    WriteToConfig(root.OuterXml);
    }
    }

    /// <summary>
    ///加载数据库中配置信息,获得xmlElement对象
    /// </summary>
    /// <returns>XmlElement对象</returns>
    private XmlElement LoadStatisticsDetailConfig(string xml)
    {
    _xmlDocument = new XmlDocument();
    string xmlContent = xml;
    _xmlDocument.LoadXml(xmlContent);
    return _xmlDocument.DocumentElement;
    }

    /// <summary>
    /// 获得每周TOP的参数信息 该方法只获得网站的域名和总访问量
    /// </summary>
    /// <returns></returns>
    public Dictionary<string, int> GetSingleWebsiteTopNodeLists(string xml)
    {
    Dictionary<string, int> allNodes = new Dictionary<string, int>();
    XmlElement xmlElement = LoadStatisticsDetailConfig(xml);
    if (xmlElement != null)
    {
    foreach (XmlNode xmlNode in xmlElement.ChildNodes)
    {
    allNodes.Add(xmlNode.Name, int.Parse(xmlNode.Attributes["value"].Value));
    }
    }
    return allNodes;
    }

  • 相关阅读:
    微信开发 接口测试
    微信开发 消息接口
    java微信学习 接入
    排序算法 java实现2
    排序算法 java实现
    第一篇博客
    Android——反编译持续完善
    Android——实用小技巧
    Android——网络编程
    Android——服务
  • 原文地址:https://www.cnblogs.com/GreenGrass/p/2920128.html
Copyright © 2011-2022 走看看