将XML反序列化为通用对象或通用类型

时间:2019-07-04 03:34:17

标签: c# asp.net xml visual-studio-2010

我具有XML文件的完整结构,有些文件将具有所有这些标记,而其他文件则没有,我需要知道的是如何在通用对象或通用类型上反序列化此结构以供使用。列表,或代码中的单个对象。

<?xml version="1.0" encoding="utf-8"?>
<monster name="Test" nameDescription="a test" race="blood" experience="1" skull="black" speed="150" manacost="25">
<health now="15" max="15"/>
<look type="136" head="1" body="1" legs="1" feet="1" addons="1" mount="136" corpse="1988"/>
<targetchange interval="2000" chance="5"/>
<strategy attack="100" defense="0"/>

<flags>
    <flag summonable="1"/>
    <flag attackable="1"/>
    <flag hostile="1"/>
    <flag illusionable="1"/>
    <flag convinceable="1"/>
    <flag pushable="1"/>
    <flag canpushitems="0"/>
    <flag canpushcreatures="0"/>
    <flag targetdistance="1"/>
    <flag staticattack="90"/>
    <flag hidehealth="0" />
    <flag lightcolor="17" />
    <flag lightlevel="1" />
    <flag runonhealth="5"/>
</flags>
<script>
    <event name="scriptName"/>
</script>
<attacks>
     <attack name="melee" interval="2000" skill="42" attack="50" poison="500"/>
     <attack name="fire" interval="2000" chance="13" length="6" spread="0" min="-150" max="-340"><!-- this represents beam -->
       <attribute key="areaEffect" value="explosion"/>
     </attack>
     <attack name="earth" interval="2000" chance="7" length="8" spread="3" min="-40" max="-220"><!-- this represents wave -->
       <attribute key="areaEffect" value="carniphila"/>
     </attack>
     <attack name="physical" interval="2000" chance="9" radius="3" range="7" target="1" min="-25" max="-90"><!-- this represents ranged bomb attack -->
       <attribute key="areaEffect" value="purpleenergy"/>
       <attribute key="shootEffect" value="energy"/>
     </attack>
     <attack name="manadrain" interval="2000" chance="24" radius="4" target="0" min="-25" max="-90"><!-- this represents bomb attack on itself -->
       <attribute key="areaEffect" value="purpleenergy"/>
     </attack>
</attacks>
<defenses armor="25" defense="52">
 <defense name="healing" interval="2000" chance="17" min="30" max="50">
   <attribute key="areaEffect" value="blueshimmer"/>
 </defense>
 <defense name="speed" interval="2000" chance="10" speedchange="336" duration="2000">
   <attribute key="areaEffect" value="redshimmer"/>
 </defense>
 <defense name="invisible" interval="2000" chance="10" duration="2000">
   <attribute key="areaEffect" value="redshimmer"/>
 </defense>
</defenses>
<elements>
    <element holyPercent="-10"/>
    <element deathPercent="-10"/>
    <element icePercent="-10"/>
    <element firePercent="10"/>
    <element earthPercent="10"/>
    <element drownPercent="10"/>
    <element energyPercent="-10"/>
    <element physicalPercent="-10"/>
    <element lifedrainPercent="10"/>
</elements>
<immunities>
    <immunity physical="1"/>
    <immunity death="1"/>
    <immunity holy="1"/>
    <immunity ice="1"/>
    <immunity energy="1"/>
    <immunity fire="1"/>
    <immunity earth="1"/>
    <immunity drown="1"/>
    <immunity lifedrain="1"/>
    <immunity paralyze="1"/>
    <immunity drunk="1"/>
    <immunity invisible="1"/>
    <immunity outfit="1"/>
</immunities>
<summons maxSummons="10">
    <summon name="rat" interval="2000" chance="10" max="6"/>
    <summon name="cave rat" interval="2000" chance="10" max="4"/>
</summons>
<voices interval="2000" chance="5">
    <voice sentence="Here's yelling with small letters." yell="1"/>
    <voice sentence="HERE'S YELLING WITH BIG LETTERS." yell="1"/>
    <voice sentence="Here's speaking with small letters."/>
    <voice sentence="HERE'S SPEAKING WITH BIG LETTERS."/>
</voices>
<loot>
    <item id="2148" countmax="5" chance="100000"/><!-- gold coin -->
    <item id="2006" subtype="4" chance="100000"/><!-- vial of slime -->
    <item id="2088" actionId="4432" chance="100000"/><!-- silver key with 4432 number -->
    <item id="2553" uniqueId="12376" chance="100000"/><!-- pick with 12376 uniqueId -->
    <item id="1950" text="Now you know how to make a book with text in monster loot" chance="100000"/><!-- book with text -->
</loot>
</monster>

我一直在尝试使用它:

public List<object> Deserialize(string input)
{
  XmlSerializer ser = new XmlSerializer(typeof(object));

  using (StreamReader sr = new StreamReader(input))
  return (List<object>)ser.Deserialize(sr);
}

但是我在(2,2)上遇到错误。

我正在使用VS2010和MVC 4中的软件包。

0 个答案:

没有答案