解析描述
定义好Xml的结构类型直接使用映射的方式去解析,不同的就够就写不同的解析方式,只需要定义接受的类的就够即可
xml的数据结构
<?xml version="1.0"?>
<Date>
<JosnName>按实际的号</JosnName>
<Size>25</Size>
<intArray>10</intArray>
<intArray>11</intArray>
<intArray>12</intArray>
<mData>
<index>0</index>
<mJson>00</mJson>
<nNickName>
<mFileName>000</mFileName>
</nNickName>
</mData>
<mData>
<index>1</index>
<mJson>11</mJson>
<nNickName>
<mFileName>111</mFileName>
</nNickName>
</mData>
</Date>
xml接受的类结构
最外层的类一定要用XmlRoot属性去标记其他嵌套的类无需标记
[XmlRoot("Date")]
public class xmlData
{
[XmlElement("JosnName")]
public string JosnName;
[XmlElement("Size")]
public int Size;
[XmlElement("intArray")]
public int[] intArray;
[XmlElement("mData")]
public xmlDateInfo[] mData;
}
public class xmlDateInfo
{
[XmlElement("index")]
public int index;
[XmlElement("mJson")]
public string mJson;
[XmlElement("nNickName")]
public NameClass nNickName;
}
public class xmlNameClass
{
[XmlElement("mFileName")]
public string mFileName;
}
调用unity的接口
解析示例
private void DemoXmlTest()
{
string temppath = $"{Application.streamingAssetsPath}/Xml/text.txt";
TextReader text = new StreamReader(temppath);
xmlData tempdata = FromXml<xmlData>(text);
}
public T FromXml<T>(TextReader varContent)
{
var serializer = new XmlSerializer(typeof(T));
var item = (T)serializer.Deserialize(varContent);
return item;
}
|