Skip to content

Commit 7bc2df8

Browse files
committed
XmlHelper
1 parent 9fc5896 commit 7bc2df8

File tree

4 files changed

+511
-0
lines changed

4 files changed

+511
-0
lines changed

Examples/XmlHelperExample.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System.Xml;
2+
using System.Xml.Serialization;
3+
using UnityEngine;
4+
5+
namespace UnityUtilities.Examples
6+
{
7+
public class XmlHelperExample : MonoBehaviour
8+
{
9+
public class TestData
10+
{
11+
// A public field - will be serialized
12+
public int field;
13+
14+
// A private field with public property - will be serialized
15+
float property;
16+
public float Property
17+
{
18+
get { return property; }
19+
set { property = value;}
20+
}
21+
22+
// An auto property - will be serialized
23+
public bool AutoProperty { get; set; }
24+
25+
// A private field - will *not* be serialized
26+
string privateField = "Test";
27+
28+
// A public field marked "XmlIgnore" - will *not* be serialized
29+
[XmlIgnore]
30+
public double publicNonSerialized = 5.5;
31+
32+
// The public default constructor is needed for the XmlSerializer.
33+
public TestData()
34+
{
35+
}
36+
37+
public TestData(int field, float property, bool autoProperty)
38+
{
39+
this.field = field;
40+
this.property = property;
41+
AutoProperty = autoProperty;
42+
}
43+
}
44+
45+
void Awake()
46+
{
47+
SerializationExamples();
48+
XmlExamples();
49+
}
50+
51+
void SerializationExamples()
52+
{
53+
// Create a new TestData object
54+
TestData data = new TestData(1, 2.3f, true);
55+
56+
// Serialize the TestData object into a string
57+
string xmlString = data.SerializeToXmlString();
58+
59+
/* Output:
60+
61+
<?xml version="1.0" encoding="utf-16"?>
62+
<TestData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
63+
<field>1</field>
64+
<Property>2.3</Property>
65+
<AutoProperty>true</AutoProperty>
66+
</TestData>
67+
*/
68+
Debug.Log(xmlString);
69+
70+
// Get the data back from the string
71+
TestData deserializedData = xmlString.DeserializeFromXmlString<TestData>();
72+
}
73+
74+
void XmlExamples()
75+
{
76+
// Create an XmlDocument with test data
77+
XmlDocument xmlDocument = new XmlDocument();
78+
xmlDocument.LoadXml("<enemyList>" +
79+
" <enemyData>" +
80+
" <name>Grunt</name>" +
81+
" <position x='5' y='3'/>" +
82+
" </enemyData>" +
83+
" <enemyData>" +
84+
" <name>Tank</name>" +
85+
" <position x='7' y='1'/>" +
86+
" <ranged>true</ranged>" +
87+
" </enemyData>" +
88+
"</enemyList>");
89+
90+
// Read each enemyData element in the enemyList
91+
foreach (XmlNode enemyData in xmlDocument["enemyList"].ChildNodes)
92+
{
93+
// Get the name element content, if it exists, else set "???"
94+
string name = enemyData.GetElementString("name", "???");
95+
96+
// Get the position element and then its attributes
97+
XmlNode position = enemyData["position"];
98+
int x = position.GetAttributeInt("x");
99+
int y = position.GetAttributeInt("y");
100+
101+
// Get the ranged element content, if it exists, else set "false"
102+
bool ranged = enemyData.GetElementBool("ranged", false);
103+
104+
// Output the result
105+
Debug.Log(string.Format("{0} at {1}|{2} is {3}",
106+
name,
107+
x,
108+
y,
109+
ranged ? "ranged" : "not ranged"));
110+
}
111+
112+
/* Grunt at 5|3 is not ranged
113+
Tank at 7|1 is ranged
114+
*/
115+
}
116+
}
117+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ If you find any bugs or have suggestions, please add an [Issue](https://github.c
1616
* [RollingArray](https://github.com/TobiasWehrum/unity-utilities/tree/master/RollingArray): Collection that keeps the last x elements that are added to it.
1717
* [Singleton](https://github.com/TobiasWehrum/unity-utilities/tree/master/Singleton): Allows easy and convenient creation of a Singleton. Optionally makes a Singleton persist between scenes while ensuring that only one exists.
1818
* [UnityHelper](https://github.com/TobiasWehrum/unity-utilities/tree/master/UnityHelper): Contains a plethora of useful extensions and helpers for Transform, GameObject, Vector2/3/4, Rect and more.
19+
* [XmlHelper](https://github.com/TobiasWehrum/unity-utilities/tree/master/XmlHelper): Serializes data to XML strings and makes accessing optional element content and attributes in general XMLs easier.
1920

2021
## Usage
2122

@@ -28,6 +29,7 @@ You can also just use selected scripts, but you should check the "Dependencies"
2829
The class documentation is available [here](http://tobiaswehrum.github.io/UnityUtilities/html/annotated.html).
2930

3031
## Changelog
32+
* 2016-06-19: Added [XmlHelper](https://github.com/TobiasWehrum/unity-utilities/tree/master/XmlHelper).
3133
* 2016-06-08: Added [UnityHelper](https://github.com/TobiasWehrum/unity-utilities/tree/master/UnityHelper).
3234
* 2016-06-05: Added [EditorHelper](https://github.com/TobiasWehrum/unity-utilities/tree/master/EditorHelper) and [RollingArray](https://github.com/TobiasWehrum/unity-utilities/tree/master/RollingArray). Added `[Tooltip]` for `NoiseOutputValue` and edited the existing `PropertyDrawer` to use tooltips.
3335
* 2016-05-15: Added [LINQExtensions](https://github.com/TobiasWehrum/unity-utilities/tree/master/LINQExtensions) and [RandomBag](https://github.com/TobiasWehrum/unity-utilities/tree/master/RandomBag).

XmlHelper/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# XmlHelper
2+
3+
The XmlHelper serializes and deserializes to/from XML and allows convenient access to optional element content and attributes when reading general XMLs.
4+
5+
With a few exceptions (e.g. arrays of ArrayList and arrays of List<T>), all public attributes and fields of any public class should be serialized without any
6+
further need to tag the elements. The only thing needed is a public default constructor.
7+
8+
For finer control, see the MSDN document on XmlSerializer: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
9+
10+
## Examples
11+
12+
### String serialization
13+
14+
```C#
15+
public class TestData
16+
{
17+
// A public field - will be serialized
18+
public int field;
19+
20+
// A private field with public property - will be serialized
21+
float property;
22+
public float Property
23+
{
24+
get { return property; }
25+
set { property = value;}
26+
}
27+
28+
// An auto property - will be serialized
29+
public bool AutoProperty { get; set; }
30+
31+
// A private field - will *not* be serialized
32+
string privateField = "Test";
33+
34+
// A public field marked "XmlIgnore" - will *not* be serialized
35+
[XmlIgnore]
36+
public double publicNonSerialized = 5.5;
37+
38+
// The public default constructor is needed for the XmlSerializer.
39+
public TestData()
40+
{
41+
}
42+
43+
public TestData(int field, float property, bool autoProperty)
44+
{
45+
this.field = field;
46+
this.property = property;
47+
AutoProperty = autoProperty;
48+
}
49+
}
50+
```
51+
52+
```C#
53+
// Create a new TestData object
54+
TestData data = new TestData(1, 2.3f, true);
55+
56+
// Serialize the TestData object into a string
57+
string xmlString = data.SerializeToXmlString();
58+
59+
/* Output:
60+
61+
<?xml version="1.0" encoding="utf-16"?>
62+
<TestData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
63+
<field>1</field>
64+
<Property>2.3</Property>
65+
<AutoProperty>true</AutoProperty>
66+
</TestData>
67+
*/
68+
Debug.Log(xmlString);
69+
70+
// Get the data back from the string
71+
TestData deserializedData = xmlString.DeserializeFromXmlString<TestData>();
72+
```
73+
74+
### XmlNode content/attributes
75+
76+
```C#
77+
// Create an XmlDocument with test data
78+
XmlDocument xmlDocument = new XmlDocument();
79+
xmlDocument.LoadXml("<enemyList>" +
80+
" <enemyData>" +
81+
" <name>Grunt</name>" +
82+
" <position x='5' y='3'/>" +
83+
" </enemyData>" +
84+
" <enemyData>" +
85+
" <name>Tank</name>" +
86+
" <position x='7' y='1'/>" +
87+
" <ranged>true</ranged>" +
88+
" </enemyData>" +
89+
"</enemyList>");
90+
91+
// Read each enemyData element in the enemyList
92+
foreach (XmlNode enemyData in xmlDocument["enemyList"].ChildNodes)
93+
{
94+
// Get the name element content, if it exists, else set "???"
95+
string name = enemyData.GetElementString("name", "???");
96+
97+
// Get the position element and then its attributes
98+
XmlNode position = enemyData["position"];
99+
int x = position.GetAttributeInt("x");
100+
int y = position.GetAttributeInt("y");
101+
102+
// Get the ranged element content, if it exists, else set "false"
103+
bool ranged = enemyData.GetElementBool("ranged", false);
104+
105+
// Output the result
106+
Debug.Log(string.Format("{0} at {1}|{2} is {3}",
107+
name,
108+
x,
109+
y,
110+
ranged ? "ranged" : "not ranged"));
111+
}
112+
113+
/* Grunt at 5|3 is not ranged
114+
Tank at 7|1 is ranged
115+
*/
116+
```
117+
118+
## Dependencies
119+
120+
None.

0 commit comments

Comments
 (0)