Regarding the case where Deserialize fails without the serialize throw of XmlSerializer.

貴(Hotmail) 21 Reputation points
2021-06-04T06:16:27.29+00:00

Regarding the case where Deserialize fails without the serialize throw of XmlSerializer.

This is a problem related to Serialize and Deserialize of XmlSerializer.

For a certain XML file, save it with [Code Flagment 1] and save it.
I am creating a program to read with [Code Flagment 2].

[Code Flagment1] is completed without entering the catch area,
When reading with [Code Flagment 2], an error of [Stack Trace] is output and There was a problem that it could not be read.

If you’re having trouble with Serialize in [Code Flagment 1],
UnknownAttribute, UnknownElement, UnknownNode, UnreferencedObject
I thought that one of the above would occur and pass the catch of [Code Flagment 1],
But, Did not pass.

This issue is not a constant issue and is very rare.
This program uses the .Net Framework 4.6.

What is the suspected cause of this problem?
I would appreciate it if you could give me some advice.
best regards.

[Code Flagment1]===============================================

try
{
serializer = new XmlSerializer("key");

using (SymmetricAlgorithm sa = new RijndaelManaged())
{
    using (ICryptoTransform encryptor = sa.CreateEncryptor(<< KeyFile's Byte[] >>, << IVFile's Byte[] >>))
    {
        lock (lockObject)
        {
            using (FileStream msEncrypt = new FileStream(<< XML File Path >>, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    serializer.Serialize(csEncrypt, settingObject);
                }
            }
        }
    }
}

}
catch (Exception e)
{
// Not Into...
throw e;

}

[code flagment2]=================================================

try
{
serializer = new XmlSerializer(objectType);

using (SymmetricAlgorithm sa = new RijndaelManaged())
{
    using (ICryptoTransform decryptor = sa.CreateDecryptor(<< KeyFile's Byte[] >>, << IVFile's Byte[] >>))
    {
        using (FileStream msDecrypt = new FileStream(<< XML File Path >>, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
        {
            if (msDecrypt.Length > 0)
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    settingObj = serializer.Deserialize(csDecrypt);
                }
            }
        }
    }
}

}
catch (System.Security.Cryptography.CryptographicException e2)
{
// Into here.
//

}

[ Stack Trace ] ======================================================

2021-02-09 09:42:41.67 SettingObject SEOJ_GSOB_0001 System.Security.Cryptography.CryptographicException パディングは無効なので、削除できません。
場所 System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
場所 System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
場所 System.Security.Cryptography.CryptoStream.FlushFinalBlock()
場所 System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
場所 System.IO.Stream.Close()
場所 System.IO.Stream.Dispose()

場所 {ユーザ作成クラス}.Settings.SettingObject.GetSettingObject(String settingFilePath, Type objectType)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
{count} votes

Accepted answer
  1. Viorel 117.6K Reputation points
    2021-06-04T08:04:36.853+00:00

    I think that you should use FileMode.Open and FileShare.Read in your deserialiser.

    Intercept the FileNotFound and other exceptions to detect that the file is missing or is currently writing.

    The serializer should use FileShare.None.

    The issue probably happens when reader tries to deserialize an incomplete file, which was not closed yet by writer.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.