c# - 我的 xml 反序列化的类层次结构有什么问题?
问题描述
我正在尝试将以下 xml 文档反序列化为 C# 对象:
<ns1:StockerFichiers
xmlns:ns1="http://www.foo.fr/bar/Repository"
xmlns:ns0="http://www.foo.fr/bar/Transport/">
<ns1:fichiersAStocker>
<ns0:FichierIdentifie>
<ns0:Contenu></ns0:Contenu>
<ns0:DomaineIdLocalDoc>128</ns0:DomaineIdLocalDoc>
<ns0:EstOriginal>true</ns0:EstOriginal>
<ns0:IdLocalDoc>2018-07-06T154554_70183_2</ns0:IdLocalDoc>
<ns0:PieceDynamique>false</ns0:PieceDynamique>
<ns0:GoldenSource>false</ns0:GoldenSource>
<ns0:TypeDoc>PDF</ns0:TypeDoc>
<ns0:TypeMime>application/pdf</ns0:TypeMime>
</ns0:FichierIdentifie>
</ns1:fichiersAStocker>
</ns1:StockerFichiers>
我知道已经存在很多反序列化问题,但即使有些似乎正在解决我面临的相同问题,我尝试过的所有问题都没有填充我的List<FichierIdentifie>
.
我反序列化的地方:
public void StockerFichiersXmlBase64(string fichiersAStocker)
{
//serializer
XmlRootAttribute xroot = new XmlRootAttribute();
xroot.ElementName = "StockerFichiers";
xroot.Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY; //ns1
XmlSerializer deserializer = new XmlSerializer(typeof(StockerFichiersRoot),xroot );
//fichiersAStocker is base64 encoded
byte[] data = Convert.FromBase64String(fichiersAStocker);
StringReader stringReader = new StringReader(Encoding.UTF8.GetString(data));
//deserialization
StockerFichiersRoot deserializedFiles = (StockerFichiersRoot)deserializer.Deserialize(stringReader);
}
我当前的版本:
// Root
[XmlRoot(ElementName = "StockerFichiers", Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY)]
public class StockerFichiersRoot
{
[XmlElement(ElementName = "fichiersAStocker", Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY)]
public FichiersAStocker fichiersAStocker { get; set; }
}
//sub root
public class FichiersAStocker
{
[XmlArray(ElementName = "fichiersAStocker", Namespace = NamespacesConstantes.NAMESPACE_SWREPOSITORY)]
[XmlArrayItem(ElementName = "FichierIdentifie", Namespace=NamespacesConstantes.NAMESPACE_MSS_TRANSPORT)]
public List<FichierIdentifie> FichiersIdentifie { get; set; }
}
public class FichierIdentifie
{
[XmlElement(Namespace = NamespacesConstantes.NAMESPACE_TRANSPORT)]
public byte[] Contenu { get; set; }
//all fields are similar to the first one
}
并且根据Is it possible to deserialize XML into List<T>? :
//sub root
public class FichiersAStocker
{
[XmlElement(ElementName = "FichierIdentifie", Namespace=NamespacesConstantes.NAMESPACE_MSS_TRANSPORT)]
public List<FichierIdentifie> FichiersIdentifie { get; set; }
}
我还尝试删除类FichiersAStocker
(子根),将其放入List<FichierIdentifie>
根类中,同时具有 [xmlArray..] 和 [XmlElement] 变体,但没有成功。
我总是得到一个列表为空的对象。
解决方案
我花了半天时间才解决的真正令人沮丧的错误:
请注意“NamespacesConstantes.NAMESPACE_MSS_TRANSPORT”如何接近“NamespacesConstantes.NAMESPACE_TRANSPORT”。添加一些惰性自动完成功能,您可以在“FichiersAStocker”类中定义 [XmlElement...] 时自欺欺人。
感谢您的帮助马特,当我将一些代码粘贴到https://dotnetfiddle.net/时,我注意到了这个错误!:)
推荐阅读
- github - Pytest 未在 GitHub 操作上找到测试
- vba - 循环获取每个类 selenium vba 的名称
- github - 如何检查我们是否是存储库的所有者?
- find - 如何找到具有相同名称的第二个 div 类?
- ruby-on-rails - 为“x.jpg”状态 500 创建 Blob 时出错
- android - 有没有办法在常规适配器中获取 Firestore 数据?
- visual-studio-code - 如何在没有 vscode 对话框的情况下直接进入定义?
- material-ui - 如何使用省略号获取截断的 html 文本?
- ios - SKPaymentTransactionObserver 未调用 updatedTransactions
- android - @UiThreadTest 未解决的参考