简体   繁体   English

在我的类层次结构中,对于XML反序列化有什么问题?

[英]What is wrong in my class hierarchy for an xml deserialization?

I'm trying to deserialize the following xml document into a C# object: 我正在尝试将以下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>

I know a lot of deserialization questions already exist, but even if some seems to be solving the same issue I face, None of what I've tried did populate my List<FichierIdentifie> . 我知道已经有很多反序列化问题,但是即使某些问题似乎正在解决我面临的同一问题,但我尝试过的任何事情都没有填充List<FichierIdentifie>

Where I deserialize: 我反序列化的地方:


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

My current version : 我当前的版本:

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

And with this variation of the subroot class according to Is it possible to deserialize XML into List<T>? 并根据的子根类的这种变化形式, 是否可以将XML反序列化为List <T>? :


//sub root
public class FichiersAStocker
{
    [XmlElement(ElementName = "FichierIdentifie", Namespace=NamespacesConstantes.NAMESPACE_MSS_TRANSPORT)]
    public List<FichierIdentifie> FichiersIdentifie { get; set; }
}

I've also tried to remove the class FichiersAStocker (the sub root), to put the List<FichierIdentifie> in the root class, with both [xmlArray..] and [XmlElement] variations but with no success. 我还尝试删除类FichiersAStocker (子根目录),将List<FichierIdentifie>放入具有[xmlArray ..]和[XmlElement]变体的根类中,但均未成功。

I always get an object with the list empty. 我总是得到一个列表为空的对象。

Try using XML2CSharp to generate class. 尝试使用XML2CSharp生成类。 Then try using that class or use it for debugging. 然后尝试使用该类或将其用于调试。

Generated code for your XML looks like this: (You can remove unwanted properties) 为XML生成的代码如下所示:(您可以删除不需要的属性)

   /* 
Licensed under the Apache License, Version 2.0

http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="FichierIdentifie",  Namespace="http://www.foo.fr/bar/Transport/")]
    public class FichierIdentifie {
    [XmlElement(ElementName="Contenu", Namespace="http://www.foo.fr/bar/Transport/")]
    public string Contenu { get; set; }
    [XmlElement(ElementName="DomaineIdLocalDoc", Namespace="http://www.foo.fr/bar/Transport/")]
    public string DomaineIdLocalDoc { get; set; }
    [XmlElement(ElementName="EstOriginal", Namespace="http://www.foo.fr/bar/Transport/")]
    public string EstOriginal { get; set; }
    [XmlElement(ElementName="IdLocalDoc", Namespace="http://www.foo.fr/bar/Transport/")]
    public string IdLocalDoc { get; set; }
    [XmlElement(ElementName="PieceDynamique", Namespace="http://www.foo.fr/bar/Transport/")]
    public string PieceDynamique { get; set; }
    [XmlElement(ElementName="SisraGoldenSource", Namespace="http://www.foo.fr/bar/Transport/")]
    public string SisraGoldenSource { get; set; }
    [XmlElement(ElementName="TypeDocSisra", Namespace="http://www.foo.fr/bar/Transport/")]
    public string TypeDocSisra { get; set; }
    [XmlElement(ElementName="TypeMime", Namespace="http://www.foo.fr/bar/Transport/")]
    public string TypeMime { get; set; }
}

[XmlRoot(ElementName="fichiersAStocker", Namespace="http://www.foo.fr/bar/Repository")]
public class FichiersAStocker {
    [XmlElement(ElementName="FichierIdentifie", Namespace="http://www.foo.fr/bar/Transport/")]
    public FichierIdentifie FichierIdentifie { get; set; }
}

[XmlRoot(ElementName="StockerFichiers", Namespace="http://www.foo.fr/bar/Repository")]
public class StockerFichiers {
    [XmlElement(ElementName="fichiersAStocker", Namespace="http://www.foo.fr/bar/Repository")]
    public FichiersAStocker FichiersAStocker { get; set; }
    [XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
    public string Ns1 { get; set; }
    [XmlAttribute(AttributeName="ns0", Namespace="http://www.w3.org/2000/xmlns/")]
    public string Ns0 { get; set; }
}

}

Really frustrating mistake that took me half a day to solve : 真令人沮丧的错误让我花了半天的时间来解决:

notice how "NamespacesConstantes.NAMESPACE_MSS_TRANSPORT" is close to "NamespacesConstantes.NAMESPACE_TRANSPORT". 请注意,“ NamespacesConstantes.NAMESPACE_MSS_TRANSPORT”如何接近“ NamespacesConstantes.NAMESPACE_TRANSPORT”。 Add some lazy autocompletion and you can fool yourself while defining the [XmlElement...] in the "FichiersAStocker" class. 添加一些自动完成功能,您就可以在定义“ FichiersAStocker”类中的[XmlElement ...]时自欺欺人。

Thanks for your help Matt, I noticed this mistake while i paste some of my code on https://dotnetfiddle.net/ ! 感谢您的帮助,Matt,当我将一些代码粘贴到https://dotnetfiddle.net/时,我注意到了这个错误! :) :)

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

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