How to Deserialize XML using DataContractSerializer

杀马特。学长 韩版系。学妹 提交于 2019-12-29 05:46:25

问题


I'm trying to deserialize an xml document:

<?xml version="1.0"?>               
<games xmlns = "http://serialize">
    <game>
        <name>TEST1</name>
        <code>TESTGAME1</code>
        <ugn>1111111</ugn>
        <bets>
            <bet>5,00</bet>
        </bets>
    </game>
    <game>
        <name>TEST2</name>
        <code>TESTGAME2</code>
        <ugn>222222</ugn>
        <bets>
            <bet>0,30</bet>
            <bet>0,90</bet>
        </bets>
    </game>
</games>

.cs class:

namespace XmlParse
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    [DataContract(Namespace = "http://serialize")]
    public class game
    {
        #region Public Properties

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public string code { get; set; }

        [DataMember]
        public long ugn { get; set; }

        [DataMember]
        public List<decimal> bets { get; set; }

        #endregion
    }

    [KnownType(typeof(game))]
    [DataContract(Namespace = "http://serialize")]
    public class games
    {
        #region Public Properties

        [DataMember]
        public List<game> game { get; set; }

        #endregion
    }
}

Main:

FileStream fs = new FileStream(Path.Combine(this.path, xmlDocumentName), FileMode.Open);

XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
DataContractSerializer ser = new DataContractSerializer(typeof(games));

// Deserialize the data and read it from the instance.
games deserializedPerson = (games)ser.ReadObject(reader, true);
reader.Close();
fs.Close();

deserializedPerson shows count = 0

what gives?


回答1:


I figured it out. Maybe there are other implementations but this works. For the life of me I couldn't find any examples that use List inside an object. Here is a working example:

XML document to parse:

<?xml version="1.0"?>               
<games xmlns = "http://serialize">
    <game>
        <name>TEST1</name>
        <code>TESTGAME1</code>
        <ugn>1111111</ugn>
        <bets>
            <bet>5,00</bet>
        </bets>
    </game>
    <game>
        <name>TEST2</name>
        <code>TESTGAME2</code>
        <ugn>222222</ugn>
        <bets>
            <bet>0,30</bet>
            <bet>0,90</bet>
        </bets>
    </game>
</games>

.cs class:

namespace XmlParse
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.Serialization;

    [DataContract(Name = "game", Namespace = "")]
    public class Game
    {
        [DataMember(Name = "name", Order = 0)]
        public string Name { get; private set; }

        [DataMember(Name = "code", Order = 1)]
        public string Code { get; private set; }

        [DataMember(Name = "ugn", Order = 2)]
        public string Ugn { get; private set; }

        [DataMember(Name = "bets", Order = 3)]
        public Bets Bets { get; private set; }
    }

    [CollectionDataContract(Name = "bets", ItemName = "bet", Namespace = "")]
    public class Bets : List<string>
    {
        public List<decimal> BetList
        {
            get
            {
                return ConvertAll(y => decimal.Parse(y, NumberStyles.Currency));
            }
        }
    }

    [CollectionDataContract(Name = "games", Namespace = "")]
    public class Games : List<Game>
    {
    }
}

Read and parse xml document:

string fileName = Path.Combine(this.path, "Document.xml");
DataContractSerializer dcs = new DataContractSerializer(typeof(Games));
FileStream fs = new FileStream(fileName, FileMode.Open);
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

Games games = (Games)dcs.ReadObject(reader);
reader.Close();
fs.Close();


来源:https://stackoverflow.com/questions/16943176/how-to-deserialize-xml-using-datacontractserializer

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