简体   繁体   English

将 XmlArrayItemAttribute 反序列化为 PascalCase 属性为 Null

[英]Deserializing XmlArrayItemAttribute to PascalCase property is Null

I am consuming a 3rd party XML API and have copied a postman response from the API and "Pasted XML As Classes" from the result into visual studio.我正在使用第 3 方 XML API,并已将来自 API 的邮递员响应和结果中的“Pasted XML As Classes”复制到 Visual Studio 中。 This gives me an object with the following property:这给了我一个具有以下属性的对象:


    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class forum : ItemBase
    {

        private forumThread[] threadsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("thread", IsNullable = false)]
        public forumThread[] threads
        {
            get
            {
                return this.threadsField;
            }
            set
            {
                this.threadsField = value;
            }
        }
    }

This deserializes just fine and captures all the data.这可以很好地反序列化并捕获所有数据。 I'd like to clean up this generated code a bit however, For example I want to rename the property name, as well as the forumList class to follow C# PascalCase conventions.但是,我想稍微清理一下生成的代码,例如,我想重命名属性名称以及forumList类以遵循 C# PascalCase 约定。 So I would like to rename threads to Threads and forumThread to ForumThread .所以我想将threads重命名为Threads并将forumThread重命名为ForumThread With other attributes/properties I've been successful at doing this by adding a name to the attribute but that doesn't work in this case, when I change the generated code to this, for example:对于其他属性/属性,我通过向属性添加名称已经成功地做到了这一点,但在这种情况下,当我将生成的代码更改为此时,这不起作用,例如:

[XmlArrayItem("thread", ElementName = "threads", IsNullable = false)]
public forumThread[] Threads {get; set;}

It deserializes to null every time and looses the data from the API call.它每次都反序列化为 null 并从 API 调用中丢失数据。 Any help on this would be greatly appreciated.对此的任何帮助将不胜感激。

The class that deserializes the object is here:反序列化对象的类在这里:

public T Deserialize<T>(string xml) 
            where T : ItemBase
        {
            if (string.IsNullOrWhiteSpace(xml))
            {
                return null;
            }

            using var stringReader = new StringReader(xml);

            var serializer = new XmlSerializer(typeof(T));            
            var xmlReader = new XmlTextReader(stringReader);

            return (T)serializer.Deserialize(xmlReader);
        }

And a sample of the xml we are deserializing:以及我们正在反序列化的 xml 示例:

<forum id="3" title="Testing Forum" numthreads="1816" numposts="13685" lastpostdate="Thu, 01 Jan 1970 00:00:00 +0000" noposting="0" termsofuse="https://boardgamegeek.com/xmlapi/termsofuse">
    <threads>
        <thread id="568813" subject="test thread" author="dakarp" numarticles="16" postdate="Tue, 28 Sep 2010 05:59:08 +0000" lastpostdate="Sat, 02 Apr 2022 11:57:08 +0000" />
        <thread id="1848343" subject="Test" author="Grafin" numarticles="1195" postdate="Thu, 14 Sep 2017 05:14:46 +0000" lastpostdate="Sun, 15 May 2022 13:39:34 +0000" />
   </threads>
</forum>

The following resolved the issue and appears to be the required attributes to get it to deserialize correctly.以下解决了该问题,并且似乎是使其正确反序列化所需的属性。 I'm not very clear on what the generated code was doing to achieve the same result, but this has solidified my preference of JSON over xml我不太清楚生成的代码做了什么来达到相同的结果,但这巩固了我对 JSON 的偏好而不是 xml

    [XmlArray("threads")]
    [XmlArrayItem("thread")]
    public forumThread[] Threads { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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