【发布时间】:2014-01-22 06:29:44
【问题描述】:
我不确定如何对存储在列表中的 XML 文件进行深层复制。我在这里阅读了一些帖子,一些使用序列化和一些使用 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>
【问题讨论】:
-
如果是资源,为什么还要深拷贝呢?为什么不加载两次?
-
哦!我想深度复制列表并遍历复制的列表以随机检索基于 id 和显示的警报。之后,已显示的警报将被删除。所以将来我可以用更多警报更新原始列表。
-
你能提供你的xml内容吗?也许小块样本然后我们可以看到元素和属性名称?
-
我已经更新了其中的 XML 内容。 pos_x,y,z 用于我希望在我的统一中弹出警报消息的位置。但目前设置为 0。
标签: c# xml list unity3d deep-copy