保存列表<>使用C#中的XML

时间:2015-05-04 11:42:39

标签: c# xml list xmlserializer

我正在研究Unity3D的组件。我有一个名为List<>的基类类型Task,并且我将从基类继承的对象添加到此列表(名为Action的子类)。我已将[XmlInclude(typeof(Action))]属性添加到我的基类中。当我将列表序列化为XML时,只有基类成员被保存到XML文件中。但我还需要一个儿童会员

//Base Class
[XmlInclude(typeof(Action))]
[XmlRoot(ElementName="Task")]
public class Task : BaseNode
{...}

//Child Class
public class Action : Task
{...}

//Program
public List<Task> list = new List<Task>();
list.Add(new Action());
list.Add(new Task());

//Serializer
void Save()
{
  using(var fileStream = new FileStream(fileDirectory, FileMode.CreateNew))
  {
    var serializer = new XmlSerializer(typeof(List<Task>));
    serializer.Serialize(fileStream, nodeList);
  }
}


XML文件

<?xml version="1.0" encoding="windows-1256"?>
<ArrayOfTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Task>
    <name />
    <hideFlags>DontSave</hideFlags>
    <wantsMouseMove>false</wantsMouseMove>
    <autoRepaintOnSceneChange>false</autoRepaintOnSceneChange>
    <maximized>false</maximized>
    <minSize>
      <x>100</x>
      <y>100</y>
    </minSize>
    <maxSize>
      <x>4000</x>
      <y>4000</y>
    </maxSize>
    <title>Task</title>
    <depthBufferBits>0</depthBufferBits>
    <antiAlias>0</antiAlias>
    <position>
      <x>0</x>
      <y>0</y>
      <position>
        <x>0</x>
        <y>0</y>
      </position>
      <center>
        <x>160</x>
        <y>120</y>
      </center>
      <min>
        <x>0</x>
        <y>0</y>
      </min>
      <max>
        <x>320</x>
        <y>240</y>
      </max>
      <width>320</width>
      <height>240</height>
      <size>
        <x>320</x>
        <y>240</y>
      </size>
      <xMin>0</xMin>
      <yMin>0</yMin>
      <xMax>320</xMax>
      <yMax>240</yMax>
    </position>
    <WindowRect>
      <x>400</x>
      <y>50</y>
      <position>
        <x>400</x>
        <y>50</y>
      </position>
      <center>
        <x>500</x>
        <y>90</y>
      </center>
      <min>
        <x>400</x>
        <y>50</y>
      </min>
      <max>
        <x>600</x>
        <y>130</y>
      </max>
      <width>200</width>
      <height>80</height>
      <size>
        <x>200</x>
        <y>80</y>
      </size>
      <xMin>400</xMin>
      <yMin>50</yMin>
      <xMax>600</xMax>
      <yMax>130</yMax>
    </WindowRect>
    <WindowTitle>root</WindowTitle>
    <Child />
    <CurrentStatus>Failed</CurrentStatus>
  </Task>
  <Task xsi:type="Action">
    <name />
    <hideFlags>DontSave</hideFlags>
    <wantsMouseMove>false</wantsMouseMove>
    <autoRepaintOnSceneChange>false</autoRepaintOnSceneChange>
    <maximized>false</maximized>
    <minSize>
      <x>100</x>
      <y>100</y>
    </minSize>
    <maxSize>
      <x>4000</x>
      <y>4000</y>
    </maxSize>
    <title>Action</title>
    <depthBufferBits>0</depthBufferBits>
    <antiAlias>0</antiAlias>
    <position>
      <x>0</x>
      <y>0</y>
      <position>
        <x>0</x>
        <y>0</y>
      </position>
      <center>
        <x>160</x>
        <y>120</y>
      </center>
      <min>
        <x>0</x>
        <y>0</y>
      </min>
      <max>
        <x>320</x>
        <y>240</y>
      </max>
      <width>320</width>
      <height>240</height>
      <size>
        <x>320</x>
        <y>240</y>
      </size>
      <xMin>0</xMin>
      <yMin>0</yMin>
      <xMax>320</xMax>
      <yMax>240</yMax>
    </position>
    <WindowRect>
      <x>195</x>
      <y>159</y>
      <position>
        <x>195</x>
        <y>159</y>
      </position>
      <center>
        <x>295</x>
        <y>199</y>
      </center>
      <min>
        <x>195</x>
        <y>159</y>
      </min>
      <max>
        <x>395</x>
        <y>239</y>
      </max>
      <width>200</width>
      <height>80</height>
      <size>
        <x>200</x>
        <y>80</y>
      </size>
      <xMin>195</xMin>
      <yMin>159</yMin>
      <xMax>395</xMax>
      <yMax>239</yMax>
    </WindowRect>
    <WindowTitle>Action</WindowTitle>
    <Child />
    <CurrentStatus>Failed</CurrentStatus>
  </Task>
</ArrayOfTask>


这是我的XML文件。在动作类中,我有一些属性,如assetID和includedScript。那些不保存在XML文件中。那些是子类属性而不是继承。

行动类(儿童)

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEditor;

public class Action : Task 
{
    [SerializeField]
    private string scriptName = "";
    [SerializeField]
    private string includedScript;
    [SerializeField]
    private string assetID;

    public Action()
        : base()
    {
        WindowTitle = "Action";
    }
    public Action(string title)
        : base(title)
    {

    }

    public Action(Rect windowRect)
        : base(windowRect)
    {
        WindowTitle = "Action";
    }

    public Action(Rect windowRect, string title)
        : base(windowRect,title)
    {

    }

    protected virtual void tick() 
    {

    }

    public override void windowDraw()
    {
        GUI.Label(new Rect(1, 15, 70, 15),"Title:");
        WindowTitle = GUI.TextField(new Rect(67, 15, 130, 15), WindowTitle);
        GUI.Label(new Rect(1, 35, 70, 15), "Class Name:");
        scriptName = GUI.TextField(new Rect(67, 35, 130, 15),scriptName);
        if (includedScript == null)
        {
            if (GUI.Button(new Rect(1, 55, 198, 20), "CreateScript"))
            {
                string codepath = BehaviorTreeEditor.classesDirectory + "/" + scriptName + ".cs";
                FileStream fileStream = new FileStream(codepath, FileMode.CreateNew);

                string[] code = File.ReadAllLines(Directory.GetCurrentDirectory().ToString() + "/Assets/Behavior Tree/Resources/Action.txt");
                for (int i = 0; i < code.Length; ++i)
                {
                    if (code[i].Contains("_"))
                    {
                        code[i] = code[i].Replace("_", scriptName);
                        break;
                    }

                }

                fileStream.Close();
                File.WriteAllLines(codepath, code);
                includedScript = codepath;
                assetID = AssetDatabase.AssetPathToGUID(codepath);
                Debug.Log(assetID);

            }
        }
        else
        {
            GUI.Label(new Rect(1, 55, 70, 15), "Script:");
            scriptName = GUI.TextField(new Rect(67, 55, 130, 15),includedScript.ToString());
        }
    }
}

实际上是一个行为树,它有不同的节点(动作,复合,根和......),我必须保存列表&lt;&gt;对于加载树可视化编辑器的统一。

0 个答案:

没有答案