上一篇讲了在Unity3D中UWP可以使用的XML的序列化的方法。
上文链接:[Unity3D]在UWP工程中使用的序列化方法。
但是XmlReader和XmlWriter使用起来很麻烦,所以本篇演示XmlDocument既可在UWP中使用又可在Unity3D编辑器中使用的方法。
UWP和Unity3D都支持XmlDocument类,但由于程序集的不同,有些在Unity3D中可以用的方法在UWP中无法使用,最终导致在编译UWP工程时失败。比如XmlDocument.Load方法在UWP中就没有通过文件路径调用的重载。
uwp 中的XmlDocument.Load:
unity3D 中的XmlDocument.Load:
所以在实际编程中,最好只使用两者都支持的方法。
下面UWP和Unity3D都支持的使用方法:
using UnityEngine;
using System.Xml;
using System.Text;
//利用宏定义区分Unity3D与UWP的引用空间
#if NETFX_CORE
using Windows.Storage;
using System.IO;
using XmlReader = WinRTLegacy.Xml.XmlReader;
using XmlWriter = WinRTLegacy.Xml.XmlWriter;
using StreamWriter = WinRTLegacy.IO.StreamWriter;
using StreamReader = WinRTLegacy.IO.StreamReader;
#else
using XmlReader = System.Xml.XmlReader;
using XmlWriter = System.Xml.XmlWriter;
using StreamWriter = System.IO.StreamWriter;
using StreamReader = System.IO.StreamReader;
#