在 XML 反序列化中将 XML 节点的值设置为 C# 模型上的字段

我希望正确反序列化一些 XML。


部分 XML 如下所示:


<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>

我想将其中的每个 KeyValue 反序列化为如下所示的 C# 对象:


public class KeyValue

{

    public string Value { get; set; }


    [XmlAttribute]

    public int Order { get; set; }


    [XmlAttribute]

    public bool Offered { get; set; }

}

这是(大致)我用来反序列化的代码:


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


using (TextReader reader = new StringReader(xml))

{

    myObject = (MyObject)serializer.Deserialize(reader);

}

这几乎可以正常工作。没有抛出异常并且正确设置了 Order 和 Offered,但我希望我的 XML 中的 KeyValues 中的一、二、三等进入我的模型的值字段。


这可能吗?如果是这样,我该怎么做?


qq_遁去的一_1
浏览 109回答 2
2回答

函数式编程

在根据 Robert Harvey 的评论查看该网站后,我意识到我缺少的是[XmlText]我的 Value 字段的属性。我补充说,测试,它工作。

潇湘沐

在这种情况下我不会使用序列化,因为它需要我的类然后其他方法。请参阅下面的 xml linq 解决方案:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;namespace ConsoleApplication110{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Key> keys = doc.Descendants("Key").Select(x => new Key()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = (string)x.Element("Name"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Values = x.Descendants("KeyValue").Select(y => new KeyValue() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = (string)y,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Offered = (bool)y.Attribute("Offered"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Order&nbsp; = (int?)y.Attribute("Order")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Key&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public List<KeyValue> Values { get;set;}&nbsp; &nbsp; }&nbsp; &nbsp; public class KeyValue&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Value { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int? Order { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public bool Offered { get; set; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP