【问题标题】:Deep Copy of XML files in a list列表中 XML 文件的深层复制
【发布时间】: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


【解决方案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; }
}

【讨论】:

  • 嗨,我试着把 get;放;对于 vectspawnpos 但它返回一个空异常。但是如果我使用新的vector3,它会说文本节点不能出现在这种状态下。我是不是做错了什么?
  • @user3128861 你应该公开 Vector3 类
猜你喜欢
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多