简体   繁体   中英

Set value of XML node as field on C# model in XML deserialization

I am looking to properly deserialize some XML.

Part of the XML looks like this:

<Keys>
    <Key>
        <Name>Test 1</Name>
        <KeyValues>
            <KeyValue Offered="true" Order="1">One</KeyValue>
            <KeyValue Offered="true" Order="2">Two</KeyValue>
            <KeyValue Offered="true" Order="3">Three</KeyValue>
            <KeyValue Offered="true" Order="4">Four</KeyValue>
        </KeyValues>                            
    </Key>                      
    <Key>
        <Name>Test 2</Name>
        <KeyValues>
            <KeyValue Offered="true">One</KeyValue>
        </KeyValues>                            
    </Key>
</Keys>

and I would like to deserialize each KeyValue from that into a C# object that looks like this:

public class KeyValue
{
    public string Value { get; set; }

    [XmlAttribute]
    public int Order { get; set; }

    [XmlAttribute]
    public bool Offered { get; set; }
}

This is (roughly) the code that I am using to deserialize:

XmlSerializer serializer = new XmlSerializer(typeof(MyObject));

using (TextReader reader = new StringReader(xml))
{
    myObject = (MyObject)serializer.Deserialize(reader);
}

This is almost working properly. No exceptions are thrown and Order and Offered are correctly set, but I'd like the One, Two, Three, etc from the KeyValues in my XML to go into the Value field on my model.

Is this possible? If so, how could I do it?

After taking a look at this website per Robert Harvey's comment, I realized that what I was missing was an [XmlText] attribute over my Value field. I added that, tested, and it worked.

I wouldn't use serialization in this case because it would require my classes then other methods. See xml linq solution below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication110
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Key> keys = doc.Descendants("Key").Select(x => new Key()
            {
                Name = (string)x.Element("Name"),
                Values = x.Descendants("KeyValue").Select(y => new KeyValue() {
                    Value = (string)y,
                    Offered = (bool)y.Attribute("Offered"),
                    Order  = (int?)y.Attribute("Order")
                }).ToList()
            }).ToList();
        }

    }
    public class Key
    {
        public string Name { get; set; }
        public List<KeyValue> Values { get;set;}
    }
    public class KeyValue
    {
        public string Value { get; set; }
        public int? Order { get; set; }
        public bool Offered { get; set; }
    }



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM