XML 格式:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <DictionaryNotes>
 3   <DictionaryNote name="CFFE-FLOOR-DM">
 4     <B_IsSuccess>True</B_IsSuccess>
 5     <LoadTime>2020/3/20 16:21:52</LoadTime>
 6     <LoadCount>1</LoadCount>
 7     <ErrorInfo>
 8     </ErrorInfo>
 9   </DictionaryNote>
10 </DictionaryNotes>

字典 格式:

 1         Dictionary<string, ModelInfo> Dic_ModelInfo = new Dictionary<string, ModelInfo>();
 2 
 3         //ModelInfo
 4         bool B_IsSuccess = false;
 5         DateTime LoadTime = DateTime.Now.AddDays(-1);
 6         int LoadCount = 0;
 7         string ErrorInfo = "";
 8         struct ModelInfo
 9         {
10             public bool B_IsSuccess;
11             public DateTime LoadTime;
12             public int LoadCount;
13             public string ErrorInfo;
14         }24         ModelInfo Set_ModelInfo(bool b_IsSuccess, DateTime loadTime, int loadCount, string errorInfo)
25         {
26             ModelInfo MI = new ModelInfo();
27             MI.B_IsSuccess = b_IsSuccess;
28             MI.LoadTime = loadTime;
29             MI.LoadCount = loadCount;
30             MI.ErrorInfo = errorInfo;
31             return MI;
32         }          

字典读取XML:

 1 private void GetXml()
 2         {  
 3             Dic_ModelInfo = new Dictionary<string, ModelInfo>();
 4             if (!File.Exists(XmlFilePath))
 5             {
 6                 return;
 7             }
 8 
 9             XDocument doc = XDocument.Load(XmlFilePath);
10             foreach (XElement element in doc.Element("DictionaryNotes").Elements())
11             {  
12                 Dic_ModelInfo.Add(element.Attribute("name").Value.ToString(),
13                     Set_ModelInfo(
14                         bool.Parse(element.Element("B_IsSuccess").Value),
15                         DateTime.Parse(element.Element("LoadTime").Value),
16                         int.Parse(element.Element("LoadCount").Value),
17                         element.Element("ErrorInfo").Value)
18                         );
19             }
20         } 

字典写入XML:

 1 private void SetXml()
 2         {
 3             if (File.Exists(XmlFilePath)) { File.Delete(XmlFilePath); } 
 4 
 5             var xmlDoc = new XmlDocument();
 6             //Create the xml declaration first  
 7             xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
 8             //Create the root node and append into doc  
 9             var el = xmlDoc.CreateElement("DictionaryNotes");
10             xmlDoc.AppendChild(el);
11 
12             foreach (var item in Dic_ModelInfo)
13             { 
14                 // DictionaryNote  
15                 XmlElement elementDictionaryNote = xmlDoc.CreateElement("DictionaryNote");  
16                 XmlAttribute attrID = xmlDoc.CreateAttribute("name");
17                 attrID.Value = item.Key;
18                 elementDictionaryNote.Attributes.Append(attrID);
19                 el.AppendChild(elementDictionaryNote);
20 
21                 // B_IsSuccess  
22                 XmlElement elementB_IsSuccess = xmlDoc.CreateElement("B_IsSuccess");
23                 elementB_IsSuccess.InnerText = item.Value.B_IsSuccess.ToString();
24                 elementDictionaryNote.AppendChild(elementB_IsSuccess);
25                 // LoadTime
26                 XmlElement elementLoadTime = xmlDoc.CreateElement("LoadTime");
27                 elementLoadTime.InnerText = item.Value.LoadTime.ToString();
28                 elementDictionaryNote.AppendChild(elementLoadTime);
29                 // LoadCount
30                 XmlElement elementLoadCount = xmlDoc.CreateElement("LoadCount");
31                 elementLoadCount.InnerText = item.Value.LoadCount.ToString();
32                 elementDictionaryNote.AppendChild(elementLoadCount);
33                 // ErrorInfo
34                 XmlElement elementErrorInfo = xmlDoc.CreateElement("ErrorInfo");
35                 elementErrorInfo.InnerText = item.Value.ErrorInfo;
36                 elementDictionaryNote.AppendChild(elementErrorInfo); 
37 
38             } 
39             xmlDoc.Save(XmlFilePath);
40     }

 

posted on 2020-03-20 16:43  _萧朗  阅读(477)  评论(0编辑  收藏  举报