gpt4 book ai didi

数组的 C# XML 反序列化

转载 作者:行者123 更新时间:2023-11-30 22:14:10 24 4
gpt4 key购买 nike

我有一个关于反序列化数组的问题。因为数组元素可以有多种类型。你可以看到这个例子:

<?xml version="1.0" encoding="UTF-8"?><export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://zakupki.gov.ru/oos/export/1" xmlns:oos="http://zakupki.gov.ru/oos/types/1">
<notificationZK>
... item 1 data
</notificationZK>
<notificationZK>
... item 2 data
</notificationZK>
<notificationFF>
... item 3 data
</notificationFF>
</export>

所有元素都扩展notificationType

   [System.Xml.Serialization.XmlIncludeAttribute(typeof(notificationSZType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(notificationPOType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(notificationZKType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(notificationEFType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(notificationOKType))]
public partial class notificationType
{
...

所以问题是如何从我的 XML 文件中获取 notificationType 元素的集合?我想我不能做类似的事情

[Serializable()]
[System.Xml.Serialization.XmlRoot("export")]
public class NotificationCollection
{
[XmlArray("")] // ???? what I need write here?
[XmlArrayItem("", typeof(notificationType))] // ??? and here?
public notificationType[] notification { get; set; }
}

问候!

已添加----------------

所以。我做这个:

[Serializable()]
[System.Xml.Serialization.XmlRoot("export")]
public class NotificationCollection
{
[XmlElement("notificationSZType", Type = typeof(notificationSZType))]
[XmlElement("notificationPOType", Type = typeof(notificationPOType))]
[XmlElement("notificationZKType", Type = typeof(notificationZKType))]
[XmlElement("notificationEFType", Type = typeof(notificationEFType))]
[XmlElement("notificationOKType", Type = typeof(notificationOKType))]
public notificationType[] notification { get; set; }
}

class Program
{
static void Main(string[] args)
{
NotificationCollection collection = null;
string path = @"E:\notification.xml";
XmlSerializer serializer = new XmlSerializer(typeof(notificationType));
StreamReader reader = new StreamReader(path);
collection = (NotificationCollection) serializer.Deserialize(reader);
reader.Close();

}
}

但是在 serializer.Deserialize(reader);

时未处理 System.InvalidOperationException
Message=<export xmlns='http://zakupki.gov.ru/oos/export/1'> not expected.

我做错了什么?

最佳答案

如何将类型声明移动到集合中?

[XmlRoot("export")]
public class NotificationCollection
{

[XmlElement("notificationZK", typeof(NotificationTypeZK))]
[XmlElement("notificationFF", typeof(NotificationTypeFF))]
public List<NotificationType> Notifications { get; set; }

}

public class NotificationType
{

}

public class NotificationTypeZK : NotificationType { }

public class NotificationTypeFF : NotificationType { }

static void Main(string[] args)
{
var data = @"<export><notificationZK /><notificationZK /><notificationFF /></export>";

var serializer = new XmlSerializer(typeof(NotificationCollection));

using (var reader = new StringReader(data))
{
var notifications = serializer.Deserialize(reader);
}
}

关于数组的 C# XML 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18631415/

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