|
| 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