gpt4 book ai didi

c# 反序列化 XML 嵌套集合

转载 作者:行者123 更新时间:2023-11-30 21:08:59 26 4
gpt4 key购买 nike

您好,我有一些 XML,我希望使用 XMLSerializer 反序列化为 .NET POCO

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>

在 xml 中,我有一个状态部分,其中包含一组操作,每个操作可能包含一组 session 。

我已经为 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; }
}
}

大多数情况下,我得到了我想要的对象图,所有值都正确反序列化了,但我找不到让 XMLSerialiser 反序列化操作元素内的 session 集合的方法 - 它们总是空的。

有谁知道我应该如何构建我的 POCO,以便 XMLserialiser 可以创建 session 集合?

最好的问候

约翰。

最佳答案

这是使用 xsd.exe 生成的。希望这会有所帮助。

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

关于c# 反序列化 XML 嵌套集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9280802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com