tangxu
2024-03-26 edd23f115dba31d764fdaf75a6207d888d0419d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
 
namespace IStation.Common
{
    public class TotalWaterOutHelper
    { 
        //读取 
        public static double Read(DateTime day)
        {
            string filePath = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
                "TotalWaterOut", day.Year.ToString(), day.Month.ToString()+".xml");
            if (!File.Exists(filePath))
            {
                return -1;
            }
 
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.Load(filePath);
            System.Xml.XmlElement rootNode = xmlDoc.DocumentElement;//根节点
            if (rootNode == null)
                return -1;
 
            XmlNode nodeInfo = rootNode.SelectSingleNode(string.Format("Day{0}", day.Day)); 
            if (nodeInfo == null)
            {
                return -1;
            }
 
            return Convert.ToDouble(nodeInfo.InnerText);
        }
 
        //保存 
        public static bool Save(DateTime day ,double sum)
        {
            var root_folder = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(), "TotalWaterOut");
            if (!System.IO.Directory.Exists(root_folder))
            {
                System.IO.Directory.CreateDirectory(root_folder);
            }
            var year_folder = System.IO.Path.Combine(root_folder, day.Year.ToString());
            if (!System.IO.Directory.Exists(year_folder))
            {
                System.IO.Directory.CreateDirectory(year_folder);
            }
 
 
            string filePath = System.IO.Path.Combine(year_folder, day.Month.ToString() + ".xml"); 
            if (!File.Exists(filePath))
            {
                XmlDocument xmlDoc1 = new XmlDocument();
 
                xmlDoc1.AppendChild(xmlDoc1.CreateXmlDeclaration("1.0", "UTF-8", null));
 
                XmlNode rootNode1 = xmlDoc1.CreateElement("root");
                xmlDoc1.AppendChild(rootNode1);
 
                xmlDoc1.Save(filePath);
            }
 
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.Load(filePath);
 
            System.Xml.XmlElement rootNode = xmlDoc.DocumentElement;//根节点
            if (rootNode == null)
                return false;
 
            XmlNode nodeInfo = rootNode.SelectSingleNode(string.Format("Day{0}", day.Day));
 
            if (nodeInfo == null)
            {
                XmlNode VersionInfoNode = xmlDoc.CreateElement(string.Format("Day{0}", day.Day));
 
                VersionInfoNode.InnerText = sum.ToString();
                rootNode.AppendChild(VersionInfoNode);
            }
            else
            { 
                nodeInfo.InnerText = sum.ToString();
            }
 
            xmlDoc.Save(filePath);
 
            return true;
        }
 
    }
}