Serializing objects in C# to XML can not change anyType to “Move”

99封情书 提交于 2019-12-25 02:22:47

问题


I am trying to get the array objects from movelist to be shown as <Move> when serializing the 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
    {
        [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();
        }
    }
}

The output when attempting to serialize it shows my move objects as <anyType psi:type="Move"> I would like them to show up as <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>

回答1:


So I dont really understand why this fixed my issue but here is what my new class looks like. If some one would like to elaborate on why this worked I would be more than happy to mark them with the answer.

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();
    }
}
}

After adding the IEnumerator IEnumerable.GetEnumerator() I am now seeing the desired results.



来源:https://stackoverflow.com/questions/20915749/serializing-objects-in-c-sharp-to-xml-can-not-change-anytype-to-move

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!