C # Problem when serializing a class

0

I have the following XML in a variable of type string.

string sCaf = "<AUTORIZACION><CAF version=\"1.0\"><DA><RE>97975000-5</RE><RS>RUT DE PRUEBA</RS><TD>33</TD><RNG><D>1</D><H>200</H></RNG><FA>2003-09-04</FA><RSAPK><M>0a4O6Kbx8Qj3K4iWSP4w7KneZYeJ+g/prihYtIEolKt3cykSxl1zO8vSXu397QhTmsX7SBEudTUx++2zDXBhZw==</M><E>Aw==</E></RSAPK><IDK>100</IDK></DA><FRMA algoritmo=\"SHA1withRSA\">g1AQX0sy8NJugX52k2hTJEZAE9Cuul6pqYBdFxj1N17umW7zG/hAavCALKByHzdYAfZ3LhGTXCai5zNxOo4lDQ==</FRMA></CAF><RSASK>-----BEGIN RSA PRIVATE KEY-----MIIBOwIBAAJBANGuDuim8fEI9yuIlkj+MOyp3mWHifoP6a4oWLSBKJSrd3MpEsZdczvL0l7t/e0IU5rF+0gRLnU1Mfvtsw1wYWcCAQMCQQCLyV9FxKFLW09yWw7bVCCdxpRDr7FRX/EexZB4VhsNxm/vtJfDZyYle0Lfy42LlcsXxPm1w6Q6NnjuW+AeBy67AiEA7iMi5q5xjswqq+49RP55o//jqdZL/pC9rdnUKxsNRMMCIQDhaHdIctErN2hCIP9knS3+9zra4R+5jSXOvI+3xVhWjQIhAJ7CF0R0S7SIHHKe04NUURf/7RvkMqm108k74sdnXi3XAiEAlkWk2vc2HM+a1sCqQxNz/098ketqe7NuidMKeoOQObMCIQCkFAMS9IcPcMjk7zI2r/4EEW63PSXyN7MFAX7TYe25mw==-----END RSA PRIVATE KEY-----</RSASK><RSAPUBK>-----BEGIN PUBLIC KEY-----MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBAOLGyNx1pFJr+mRE2O8NoJGtJFvEvqJMWwgOi5wc9lIBtBmgQHRjXnSt0FWwPdrR+Yv/ACSDf/0sI2k1juom0oUCAQM=-----END PUBLIC KEY-----\r\n</RSAPUBK></AUTORIZACION>"

Then I deserialize it correctly to an object, I verify some properties of the class, especially the properties of type byte []

AUTORIZACION autorizacion;
XmlSerializer serializer = new XmlSerializer( typeof(AUTORIZACION) );

autorizacion = DTEBussinesLayer.DBClass.Xml.XmlDeserializeFromString<AUTORIZACION>( sCaf );

for example the value of:

autorizacion.CAF[0].FRMA[0].Value 

is:

"g1AQX0sy8NJugX52k2hTJEZAE9Cuul6pqYBdFxj1N17umW7zG/hAavCALKByHzdYAfZ3LhGTXCai5zNxOo4lDQ=="

Then I assign the value to a Class called DTEDefTypeDocumentoTEDDDCAFDARSAPK in the following way:

DTEDefTypeDocumentoTEDDDCAFDARSAPK rsapk = new DTEDefTypeDocumentoTEDDDCAFDARSAPK();

rsapk.M = Encoding.GetEncoding("ISO-8859-1").GetBytes( autorizacion.CAF[0].DA[0].RSAPK[0].M );
rsapk.E = Encoding.GetEncoding("ISO-8859-1").GetBytes( autorizacion.CAF[0].DA[0].RSAPK[0].E );

The values of M and E of the instance rsapk are of type byte [] so I convert from authorization.CAF [0] .DA [0] .RSAPK [0] .M and autorizacion.CAF [0] .DA [0] .RSAPK [0] .E to byte [] (they are as in string in authorization).

rsapk.M = Encoding.GetEncoding("ISO-8859-1").GetBytes( autorizacion.CAF[0].DA[0].RSAPK[0].M );

Finally I assign another class that contains this data:

CAF.DA.Item = rsapk;

So far so good. But after serializing the object called caf, a problem arises during the serialization. And is that although the byte arrays are correctly assigned after serializing bytes type fields the strings formed for M, E, FRMA change and are no longer the same.

The serialization was done in this way:

string sCaf = DTEBussinesLayer.DBClass.Xml.Serialize( caf, "CAF" );
sCaf = DTEBussinesLayer.DBClass.Xml.Serialize( caf );

Before serializing I have this in the class array:

The result is the following, but when reviewing M, E, and FRMA I see that the values are totally different from those in the serialized class.

<CAF version="1.0">
<DA>
    <RE>97975000-5</RE>
    <RS>RUT DE PRUEBA</RS>
    <TD>33</TD>
    <RNG>
        <D>1</D>
        <H>200</H>
    </RNG>
    <FA>2003-09-04</FA>
    <RSAPK>
        <M>MGE0TzZLYng4UWozSzRpV1NQNHc3S25lWlllSitnL3ByaWhZdElFb2xLdDNjeWtTeGwxek84dlNYdTM5N1FoVG1zWDdTQkV1ZFRVeCsrMnpEWEJoWnc9PQ==</M>
        <E>QXc9PQ==</E>
    </RSAPK>
    <IDK>100</IDK>
</DA>
<FRMA algoritmo="SHA1withRSA">ZzFBUVgwc3k4Tkp1Z1g1MmsyaFRKRVpBRTlDdXVsNnBxWUJkRnhqMU4xN3VtVzd6Ry9oQWF2Q0FMS0J5SHpkWUFmWjNMaEdUWENhaTV6TnhPbzRsRFE9PQ==</FRMA>

Can someone guide me on what my problem is?

Here are the classes that contain the types of data: link

This is the class I use to deserialize:

    public static class Xml
{
    #region Fields 

    private static readonly XmlWriterSettings WriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false, Encoding = Encoding.GetEncoding("ISO-8859-1") };
    private static readonly XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") } );

    #endregion

    #region Methods 

    public static string Serialize(object obj, string RootElement = null)
    {

        if (obj == null)
        {
            return null;
        }

        return DoSerialize(obj, RootElement);
    }

    private static string DoSerialize(object obj, string RootElement)
    {
        using (var ms = new MemoryStream())
        using (var writer = XmlWriter.Create(ms, WriterSettings))
        {

            if (RootElement != null)
            {
                var xroot = new XmlRootAttribute(RootElement);
                var xattribs = new XmlAttributes();
                xattribs.Xmlns = false;
                xattribs.XmlRoot = xroot;

                var xoverrides = new XmlAttributeOverrides();
                xoverrides = new XmlAttributeOverrides();
                xoverrides.Add(obj.GetType(), xattribs);

                var xSer2 = new XmlSerializer(obj.GetType(), xoverrides);
                xSer2.Serialize(writer, obj, Namespaces);

            }
            else
            {
                var serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(writer, obj, Namespaces);
            }
            XmlDocument xdoc = new XmlDocument();

            // Remover todos los xlmns
            xdoc = RemoveXmlns(Encoding.GetEncoding("ISO-8859-1").GetString( ms.ToArray()) );

            return xdoc.InnerXml;

        }
    }

    public static XmlDocument RemoveXmlns(String xml)
    {
        XDocument d = XDocument.Parse(xml);
        d.Root.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();

        foreach (var elem in d.Descendants())
            elem.Name = elem.Name.LocalName;

        var xmlDocument = new XmlDocument();
        xmlDocument.Load(d.CreateReader());

        return xmlDocument;
    }
   #endregion
}
    
asked by José Donoso 28.09.2018 в 19:17
source

1 answer

0

JuanK, Thanks for responding. Here in the following link are the classes: complete:

link

The Authorization.cs File contains:

  • AUTHORIZATION
  • AUTHORIZATIONCAF
  • AUTHORIZATIONCAFDA
  • AUTHORIZATIONCAFDARNG
  • AUTHORIZATIONCAFDARNGRSAPK
  • AUTHORIZATIONCAFFRMA

I enclose an image explaining the problem visually:

If you look at the first red box, I highlight the data from the byte array "M" if I decode the codes and digit them the string that I start to form is different and that is what it should be.

M="6xv686 ....."

But decode them to:

M="Nnh2Nj ...." as shown in the second box.

I enclose the functions with which Serializo:

    private static readonly XmlWriterSettings WriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false, Encoding = Encoding.GetEncoding("ISO-8859-1") };
    private static readonly XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") } );
    public static string Serialize(object obj, string RootElement = null)
    {

        if (obj == null)
        {
            return null;
        }

        return DoSerialize(obj, RootElement);
    }

    private static string DoSerialize(object obj, string RootElement)
    {
        using (var ms = new MemoryStream())
        using (var writer = XmlWriter.Create(ms, WriterSettings))
        {

            if (RootElement != null)
            {
                var xroot = new XmlRootAttribute(RootElement);
                var xattribs = new XmlAttributes();
                xattribs.Xmlns = false;
                xattribs.XmlRoot = xroot;

                var xoverrides = new XmlAttributeOverrides();
                xoverrides = new XmlAttributeOverrides();
                xoverrides.Add(obj.GetType(), xattribs);

                var xSer2 = new XmlSerializer(obj.GetType(), xoverrides);
                xSer2.Serialize(writer, obj, Namespaces);

            }
            else
            {
                var serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(writer, obj, Namespaces);
            }
            XmlDocument xdoc = new XmlDocument();

            // Remover todos los xlmns
            xdoc = RemoveXmlns(Encoding.GetEncoding("ISO-8859-1").GetString( ms.ToArray()) );

            return xdoc.InnerXml;

        }
    }

    public static XmlDocument RemoveXmlns(String xml)
    {
        XDocument d = XDocument.Parse(xml);
        d.Root.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();

        foreach (var elem in d.Descendants())
            elem.Name = elem.Name.LocalName;

        var xmlDocument = new XmlDocument();
        xmlDocument.Load(d.CreateReader());

        return xmlDocument;
    }
    
answered by 03.10.2018 в 05:56