-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLController.cs
More file actions
94 lines (84 loc) · 3.36 KB
/
XMLController.cs
File metadata and controls
94 lines (84 loc) · 3.36 KB
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
94
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using JetBrains.Annotations;
using UnityEngine;
public class XMLController
{
/// <summary>
/// Load XML
/// </summary>
/// <param name="filePath">file exist path</param>
/// <param name="header">Xml header string</param>
/// <param name="strKeys">Search key</param>
/// <returns>dictionary data</returns>
public static Dictionary<string, string> LoadData(string filePath,string header,string[] strKeys)
{
var dataDictionary = new Dictionary<string, string>(); // Load Data
var stringBuilder = new StringBuilder();
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
var fileName = Application.streamingAssetsPath + '/' + filePath; // Use StreamingAsset folder
#endif
using (var reader = XmlReader.Create(fileName))
{
// Read all data
while( reader.Read() )
{
if ( reader.Name.Equals(header) )
{
reader.Read();
foreach( var key in strKeys )
{
stringBuilder.Clear();
stringBuilder.Append(reader.ReadElementString(key, ""));
dataDictionary.Add(key, stringBuilder.ToString());
}
}
}
}
return dataDictionary;
}
/// <summary>
/// Create Xml file
/// </summary>
/// <param name="fileName">Save file name</param>
/// <param name="xmlDictionary">Save Data</param>
/// <param name="strStartElement">Xml start element</param>
/// <param name="strAttribute">Xml attribute</param>
/// <param name="strAttributeValue">Xml attribute value</param>
/// <param name="strKeys">Save data's key</param>
public static void CreateXmlFile(string fileName, Dictionary<string, string> xmlDictionary, string[] strKeys,
string strStartElement, [CanBeNull] string strAttribute = null, [CanBeNull] string
strAttributeValue = null)
{
// None file Exception
if ( fileName == null || xmlDictionary.Count <= 0 )
{
Debug.LogError("FileName : " + (string.IsNullOrEmpty(fileName) ? fileName : "NULL") +
"Data : " + (xmlDictionary.Count <= 0 ? xmlDictionary.ToString() : "Empty"));
return;
}
#if ( UNITY_EDITOR || UNITY_STANDALONE_WIN )
var filePath = Application.streamingAssetsPath + "/" + fileName;
#endif
// instancing xml writer
using (var writer = XmlWriter.Create(filePath))
{
// Write start
writer.WriteStartDocument();
writer.WriteStartElement(strStartElement);
// Attribute option
if(strAttribute != null && strAttributeValue != null)
writer.WriteAttributeString(strAttribute,strAttributeValue);
foreach( var key in strKeys )
{
// Write data
writer.WriteElementString(key, xmlDictionary[key]);
}
writer.WriteEndElement();
// Write end
writer.WriteEndDocument();
}
}
}