将 C# 中的对象序列化为 XML 不能将任何类型更改为"Move"



我正在尝试从移动列表中获取数组对象,以便在序列化移动列表时显示为<Move>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Xml.Serialization;
namespace Arbitrary.Parties.Moves
{
    [XmlType("MoveList")]
    [XmlInclude(typeof(Move))]
    public class MoveList : IEnumerable
    {
        [XmlArrayItem(typeof(Move))]
        public List<Move> oMoveList = new List<Move>();
        public MoveList()
        {
        }
        public void Add(object o)
        {
            this.oMoveList.Add(o as Move); 
        }
        public int Count()
        {
             return oMoveList.Count;
        }
        public IEnumerator GetEnumerator()
        {
            return oMoveList.GetEnumerator();
        }
    }
}

尝试序列化它时的输出显示我的移动对象<anyType psi:type="Move">我希望它们显示为<Move>

<?xml version="1.0" encoding="utf-16"?>
  <MoveList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <anyType xsi:type="Move">
    <MoveName element="normal" order="1" rating="green" threshold="0">Wild Swing</MoveName>
    <MoveLevel element="normal" order="1" rating="green" threshold="0">1</MoveLevel>
    <MoveAOEDamage element="normal" link="1" order="1" rating="green" stat="strength" target="relative(1,1|1,0|1,-1)" threshold="0">2</MoveAOEDamage>
    <MoveDescription element="normal" link="1" order="1" rating="green" threshold="0">VARACTINGCHARACTER takes a VARMOVENAME at VARTARGET1, VARTARGET2, VARTARGET3!         </MoveDescription>
    <MoveDescription element="normal" link="1" order="2" rating="green" threshold="0">VARTARGET1 takes VARDAMAGE1 damage!</MoveDescription>
 </anyType>
 <anyType xsi:type="Move">
    <MoveName element="normal" order="1" rating="green" threshold="0">Overpower</MoveName>
    <MoveLevel element="normal" order="1" rating="green" threshold="0">1</MoveLevel>
    <MoveAmplify duration="1" element="normal" link="1" order="1" rating="green" target="enemy primary" threshold="0">25</MoveAmplify>
    <MoveDescription element="normal" link="1" order="1" rating="green" threshold="0">VARACTINGCHARACTER VARMOVENAMEs VARTARGET1's defenses!</MoveDescription>
    <MoveDescription element="normal" link="1" order="2" rating="green" threshold="0">VARTARGET1 takes VARMOVEAMPLIFY1% more damage!</MoveDescription>
 </anyType>
 <anyType xsi:type="Move">
   <MoveName element="normal" order="1" rating="green" threshold="0">Patch Up</MoveName>
   <MoveLevel element="normal" order="1" rating="green" threshold="0">1</MoveLevel>
   <MoveHeal element="normal" link="1" order="1" rating="green" stat="agility" target="self" threshold="0">1</MoveHeal>
   <MoveDescription element="normal" link="1" order="1" rating="green" threshold="0">VARACTINGCHARACTER tries to patch up VARTARGET1's wounds!</MoveDescription>
   <MoveDescription element="normal" link="1" order="2" rating="green" threshold="0">VARTARGET1 is healed for VARHEAL1!</MoveDescription>
 </anyType>
 </MoveList>
所以我

真的不明白为什么这解决了我的问题,但这是我的新类的样子。如果有人想详细说明为什么这有效,我将非常乐意用答案标记他们。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Xml.Serialization;
namespace Arbitrary.Parties.Moves
{
    [XmlType("MoveList")]
    [XmlInclude(typeof(Move))]
public class MoveList : IEnumerable<Move>
{
    [XmlArrayItem(typeof(Move))]
    public List<Move> oMoveList = new List<Move>();
    public MoveList()
    {
    }
    public void Add(Move move)
    {
        this.oMoveList.Add(move); 
    }
    public int Count()
    {
        return oMoveList.Count;
    }
    public IEnumerator<Move> GetEnumerator()
    {
        return oMoveList.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
}

添加IEnumerator IEnumerable.GetEnumerator()后,我现在看到了预期的结果。

最新更新