简体   繁体   English

C#反序列化XML嵌套集合

[英]c# Deserialize XML nested collections

Hi I have some XML I wish to deserialise to .NET POCOs using the XMLSerializer 嗨,我有一些XML,我希望使用XMLSerializer反序列化为.NET POCO。

the xml looks like this: xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
  <message uuid="{2f1e274c-6a53-afea-3047-6dc739539656}">
    <envelope received="a_date" subject="a_name">
      <from>
        <monitor name="Local Folder" user_description="" uuid="{668DC658-97D7-42c8-AE72-ED289DD02355}"/>
      </from>
      <to>
        <account>
          <factory name="a_name"/>
        </account>
      </to>
    </envelope>
    <status>
      <action name="Folder" occured="a_date" type="monitor">
        <session completed="a_date" name="a_name" started="a_date"/>
      </action>
      <action occured="a_date" type="monitor"/>
      <action occured="a_date" type="translate">
        <session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="time"/>
        <session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="time"/>
      </action>
      <action occured="a_date" type="deliver">
        <session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="byte"/>
        <session completed="a_date" name="a_name" started="a_date" unit="byte"/>
        <session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="byte"/>
      </action>
      <action occured="a_date" type="complete"/>
    </status>
    <host name="a_name"/>
</message>

Within the xml, I have a status section which contains a collection of actions, each action may contain a collection of sessions. 在xml中,我有一个status部分,其中包含动作的集合,每个动作可能包含会话的集合。

I have created classes for the XMLSerialiser to deserialize the xml: 我已经为XMLSerialiser创建了反序列化xml的类:

namespace myNameSpace
{
    [XmlRoot("message")]
    public class message
    {
        [XmlAttribute("uuid")] 
        public string uuid { get; set; }
        [XmlElement("envelope")]
        public envelope envelope { get; set; }
        [XmlArray("status")]
        [XmlArrayItem(typeof(action))]
        public ObservableCollection<action> status { get; set; }
        [XmlElement("host")]
        public host host { get; set; }
    }

    public class envelope
    {
        [XmlAttribute("received")] 
        public string received { get; set; }
        [XmlAttribute("subject")]
        public string subject { get; set; }
        [XmlElement("from")]
        public from from { get; set; }
        [XmlElement("to")]
        public to to { get; set; }
    }

    #region envelope element definitions

    public class from
    {
        [XmlElement("monitor")]
        public monitor monitor { get; set; }

    }

    public class monitor
    {
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlAttribute("user_description")]
        public string user_description { get; set; }
        [XmlAttribute("uuid")]
        public string uuid { get; set; }

    }

    public class to
    {
        [XmlElementAttribute("account")]
        public account account { get; set; }
    }

    public class account
    {
        [XmlElementAttribute("factory")]
        public factory factory { get; set; }
    }

    public class factory
    {
        [XmlAttribute("name")]
        public string name { get; set; }
    }

    #endregion

    public class action
    {
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlAttribute("occured")]
        public string occured { get; set; }
        [XmlAttribute("type")]
        public string type { get; set; }
        [XmlArray("action")]
        [XmlArrayItem(typeof(session))]
        public ObservableCollection<session> session { get; set; }
    }

    public class session
    {
        [XmlAttribute("completed")]
        public string completed { get; set; }
        [XmlAttribute("current")]
        public long current { get; set; }
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlAttribute("started")]
        public string started { get; set; }
        [XmlAttribute("total")]
        public long total { get; set; }
        [XmlAttribute("unit")]
        public string unit { get; set; }
    }

    public class host
    {
        [XmlAttribute("name")]
        public string name { get; set; }
    }
}

Mostly I get the object graph I desire with all the values correctly deserialzed, but I can not find a way to get the XMLSerialiser to deserialize the session collection within an action element - The are always empty. 通常,我得到的对象图具有正确反序列化的所有值,但是我找不到一种方法来使XMLSerialiser对操作元素内的会话集合进行反序列化-它们始终为空。

Does anyone know how I might build my POCOs so that the XMLserialiser can create the session collections? 有谁知道我该如何构建我的POCO,以便XMLserialiser可以创建会话集合?

best regards 最好的祝福

John. 约翰。

This was generated using xsd.exe. 这是使用xsd.exe生成的。 Hope this helps. 希望这可以帮助。

using System.Xml.Serialization;
using System.Xml.Schema;
using System;

[SerializableAttribute()]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class message
{
    [XmlElementAttribute("envelope", Form = XmlSchemaForm.Unqualified)]
    public messageEnvelope[] envelope { get; set; }

    [XmlArrayAttribute(Form = XmlSchemaForm.Unqualified)]
    [XmlArrayItemAttribute("action", typeof(messageStatusAction), Form = XmlSchemaForm.Unqualified, IsNullable = false)]
    public messageStatusAction[][] status { get; set; }

    [XmlElementAttribute("host", Form = XmlSchemaForm.Unqualified)]
    public messageHost[] host { get; set; }

    [XmlAttributeAttribute()]
    public string uuid { get; set; }
}

[SerializableAttribute()]
public partial class messageEnvelope
{
    [XmlArrayAttribute(Form = XmlSchemaForm.Unqualified)]
    [XmlArrayItemAttribute("monitor", typeof(messageEnvelopeFromMonitor), Form = XmlSchemaForm.Unqualified, IsNullable = false)]
    public messageEnvelopeFromMonitor[][] from { get; set; }

    [XmlArrayAttribute(Form = XmlSchemaForm.Unqualified)]
    [XmlArrayItemAttribute("account", typeof(messageEnvelopeTOAccountFactory[]), Form = XmlSchemaForm.Unqualified, IsNullable = false)]
    [XmlArrayItemAttribute("factory", typeof(messageEnvelopeTOAccountFactory), Form = XmlSchemaForm.Unqualified, IsNullable = false, NestingLevel = 1)]
    public messageEnvelopeTOAccountFactory[][][] to { get; set; }

    [XmlAttributeAttribute()]
    public string received { get; set; }

    [XmlAttributeAttribute()]
    public string subject { get; set; }
}

[SerializableAttribute()]
public partial class messageEnvelopeFromMonitor
{
    [XmlAttributeAttribute()]
    public string name { get; set; }

    [XmlAttributeAttribute()]
    public string user_description { get; set; }

    [XmlAttributeAttribute()]
    public string uuid { get; set; }
}

[SerializableAttribute()]
public partial class messageEnvelopeTOAccountFactory
{
    [XmlAttributeAttribute()]
    public string name { get; set; }
}

[SerializableAttribute()]
public partial class messageStatusAction
{
    [XmlElementAttribute("session", Form = XmlSchemaForm.Unqualified)]
    public messageStatusActionSession[] session { get; set; }

    [XmlAttributeAttribute()]
    public string name { get; set; }

    [XmlAttributeAttribute()]
    public string occured { get; set; }

    [XmlAttributeAttribute()]
    public string type { get; set; }
}

[SerializableAttribute()]
public partial class messageStatusActionSession
{
    [XmlAttributeAttribute()]
    public string completed { get; set; }

    [XmlAttributeAttribute()]
    public string name { get; set; }

    [XmlAttributeAttribute()]
    public string started { get; set; }

    [XmlAttributeAttribute()]
    public string current { get; set; }

    [XmlAttributeAttribute()]
    public string total { get; set; }

    [XmlAttributeAttribute()]
    public string unit { get; set; }
}

[SerializableAttribute()]
public partial class messageHost
{
    [XmlAttributeAttribute()]
    public string name { get; set; }
}

[SerializableAttribute()]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class NewDataSet
{
    [XmlElementAttribute("message")]
    public message[] Items { get; set; }
}

I believe your problem is once you are at the action level, you cant specify it as the XmlArray Type. 我相信您的问题是,一旦您处于操作级别,就不能将其指定为XmlArray类型。 That would be the sessions. 那就是会议。 Give this a try and see if it works: 尝试一下,看看是否可行:

public class action
{
    [XmlAttribute("name")]
    public string name { get; set; }
    [XmlAttribute("occured")]
    public string occured { get; set; }
    [XmlAttribute("type")]
    public string type { get; set; }
    [XmlArray(typeof(session))]
    public ObservableCollection<session> session { get; set; }
}

First, session must inherit action . 首先, session必须继承action

Then you might need to replace public ObservableCollection<action> status { get; set; } 然后,您可能需要替换public ObservableCollection<action> status { get; set; } public ObservableCollection<action> status { get; set; } public ObservableCollection<action> status { get; set; } by public List<action> status { get; set; } public ObservableCollection<action> status { get; set; }通过public List<action> status { get; set; } public List<action> status { get; set; } public List<action> status { get; set; } or public action[] status { get; set; } public List<action> status { get; set; }public action[] status { get; set; } public action[] status { get; set; } public action[] status { get; set; } (I don't if ObservableCollection is considered XmlSerializable). public action[] status { get; set; } (如果ObservableCollection被视为XmlSerializable,则不会这样)。

Finally, you may have to add the [XmlInclude(typeof(session))] on the action class declaration. 最后,您可能必须在action类声明上添加[XmlInclude(typeof(session))]

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

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