我不确定如何对存储在列表中的XML文件进行深层复制。我在这里阅读了一些帖子,有些人使用了Serialize&一些使用Icloneable。哪种方法最好?
目前我只阅读了XML文件并将其存储在列表中。
TextAsset XMLFile;
public GameObject spawnAlert;
List<AlertInfo> m_AlertInfo = new List<AlertInfo>();
public class AlertInfo
{
public string strId { get; set; }
public string strName { get; set; }
public Vector3 vectSpawnPos = new Vector3();
}
void Start()
{
TextAsset textXML = (TextAsset)Resources.Load("Alert", typeof(TextAsset));
XmlDocument xml = new XmlDocument();
xml.LoadXml(textXML.text);
XmlNode root = xml.FirstChild;
for( int x = 0; x < root.ChildNodes.Count; x++)
{
AlertInfo alertI = new AlertInfo();
alertI.strId = root.ChildNodes[x].Attributes[0].Value;
alertI.strName = root.ChildNodes[x].Attributes[1].Value;
alertI.vectSpawnPos.x = float.Parse( root.ChildNodes[x].Attributes[2].Value);
alertI.vectSpawnPos.y = float.Parse( root.ChildNodes[x].Attributes[3].Value);
alertI.vectSpawnPos.z = float.Parse( root.ChildNodes[x].Attributes[4].Value);
m_AlertInfo.Add( alertI );
}
<ListOfAlerts>
<AlertInfo id = "1" Name = "Unsecured Aircraft" pos_X = "0" pos_Y = "0" pos_Z = "0" />
<AlertInfo id = "2" Name = "Suspecious Baggage Found at Lobby" pos_X = "0" pos_Y = "0" pos_Z = "0" />
<AlertInfo id = "3" Name = "Disruptive person at Departure Hall" pos_X = "0" pos_Y = "0" pos_Z = "0" />
<AlertInfo id = "4" Name = "Suspicious person loitering around at Arrival Hall" pos_X = "0"pos_Y = "0" pos_Z = "0" />
</ListOfAlerts>
答案 0 :(得分:1)
如何使用LINQ to XML
?
XDocument xDoc = XDocument.Parse("textXML.text");
var myList = xDoc.Descendants("AlertInfo").Select(x => new AlertInfo()
{
strId = (string) x.Attribute("id"),
strName = (string) x.Attribute("Name"),
vectSpawnPos = new Vector3
{
x = (float) x.Attribute("pos_X"),
y = (float) x.Attribute("pos_Y"),
z = (float) x.Attribute("pos_Z")
}
}).ToList();
要使用此代码,您需要进行一些更改。使用vectSpawnPos
自动实现的属性,例如:
public Vector3 vectSpawnPos { get; set; }
您的Vector3
课程应如下所示:
public class Vector3
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}