Is C# .NET Serializer Atomic?











up vote
-2
down vote

favorite












Straight forward question. If CPU crashes during serialization, will the file be corrupted?



Example of .NET Serialization C#:



    public bool TrySerializeToXML<T>(String FilePath, T t, XmlAttributeOverrides XmlAttributeOverrides = null)
{
lock (SerializationLock)
{
XmlSerializer Serializer = null;
try
{
if (XmlAttributeOverrides == null) Serializer = new XmlSerializer(t.GetType());
else Serializer = new XmlSerializer(t.GetType(), XmlAttributeOverrides);
using (TextWriter TextWriter = new StreamWriter(FilePath))
{
Serializer.Serialize(TextWriter, t);
}
}
catch
{
return false;
}
return true;
}
}









share|improve this question


















  • 6




    The CPU never crashes, it merely tells you what you did wrong when you use C#. Use C++ if you favor an undiagnosable crash that nobody can help you with.
    – Hans Passant
    Nov 21 at 23:59










  • Thanks for the reply. Yes i understand... jeez.. common sense. I mean anything that happens during the operation will it cause the written file to be corrupted? or partially written
    – user1034912
    Nov 22 at 0:03






  • 3




    The file will be corru
    – Hans Passant
    Nov 22 at 0:10






  • 1




    That was a lot funnier then I intended it to be, sorry. My wife give me funny looks. I ought to be more helpful: use transactional saves. First save to a file with a temporary name. If nothing went wrong then rename it to the file it should be. The File.Replace() overload with three arguments is important to get this right.
    – Hans Passant
    Nov 22 at 0:21












  • There should be League Tables of comments of the day, i think that would win for today
    – TheGeneral
    Nov 22 at 0:24

















up vote
-2
down vote

favorite












Straight forward question. If CPU crashes during serialization, will the file be corrupted?



Example of .NET Serialization C#:



    public bool TrySerializeToXML<T>(String FilePath, T t, XmlAttributeOverrides XmlAttributeOverrides = null)
{
lock (SerializationLock)
{
XmlSerializer Serializer = null;
try
{
if (XmlAttributeOverrides == null) Serializer = new XmlSerializer(t.GetType());
else Serializer = new XmlSerializer(t.GetType(), XmlAttributeOverrides);
using (TextWriter TextWriter = new StreamWriter(FilePath))
{
Serializer.Serialize(TextWriter, t);
}
}
catch
{
return false;
}
return true;
}
}









share|improve this question


















  • 6




    The CPU never crashes, it merely tells you what you did wrong when you use C#. Use C++ if you favor an undiagnosable crash that nobody can help you with.
    – Hans Passant
    Nov 21 at 23:59










  • Thanks for the reply. Yes i understand... jeez.. common sense. I mean anything that happens during the operation will it cause the written file to be corrupted? or partially written
    – user1034912
    Nov 22 at 0:03






  • 3




    The file will be corru
    – Hans Passant
    Nov 22 at 0:10






  • 1




    That was a lot funnier then I intended it to be, sorry. My wife give me funny looks. I ought to be more helpful: use transactional saves. First save to a file with a temporary name. If nothing went wrong then rename it to the file it should be. The File.Replace() overload with three arguments is important to get this right.
    – Hans Passant
    Nov 22 at 0:21












  • There should be League Tables of comments of the day, i think that would win for today
    – TheGeneral
    Nov 22 at 0:24















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Straight forward question. If CPU crashes during serialization, will the file be corrupted?



Example of .NET Serialization C#:



    public bool TrySerializeToXML<T>(String FilePath, T t, XmlAttributeOverrides XmlAttributeOverrides = null)
{
lock (SerializationLock)
{
XmlSerializer Serializer = null;
try
{
if (XmlAttributeOverrides == null) Serializer = new XmlSerializer(t.GetType());
else Serializer = new XmlSerializer(t.GetType(), XmlAttributeOverrides);
using (TextWriter TextWriter = new StreamWriter(FilePath))
{
Serializer.Serialize(TextWriter, t);
}
}
catch
{
return false;
}
return true;
}
}









share|improve this question













Straight forward question. If CPU crashes during serialization, will the file be corrupted?



Example of .NET Serialization C#:



    public bool TrySerializeToXML<T>(String FilePath, T t, XmlAttributeOverrides XmlAttributeOverrides = null)
{
lock (SerializationLock)
{
XmlSerializer Serializer = null;
try
{
if (XmlAttributeOverrides == null) Serializer = new XmlSerializer(t.GetType());
else Serializer = new XmlSerializer(t.GetType(), XmlAttributeOverrides);
using (TextWriter TextWriter = new StreamWriter(FilePath))
{
Serializer.Serialize(TextWriter, t);
}
}
catch
{
return false;
}
return true;
}
}






c# .net serialization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 23:56









user1034912

73622335




73622335








  • 6




    The CPU never crashes, it merely tells you what you did wrong when you use C#. Use C++ if you favor an undiagnosable crash that nobody can help you with.
    – Hans Passant
    Nov 21 at 23:59










  • Thanks for the reply. Yes i understand... jeez.. common sense. I mean anything that happens during the operation will it cause the written file to be corrupted? or partially written
    – user1034912
    Nov 22 at 0:03






  • 3




    The file will be corru
    – Hans Passant
    Nov 22 at 0:10






  • 1




    That was a lot funnier then I intended it to be, sorry. My wife give me funny looks. I ought to be more helpful: use transactional saves. First save to a file with a temporary name. If nothing went wrong then rename it to the file it should be. The File.Replace() overload with three arguments is important to get this right.
    – Hans Passant
    Nov 22 at 0:21












  • There should be League Tables of comments of the day, i think that would win for today
    – TheGeneral
    Nov 22 at 0:24
















  • 6




    The CPU never crashes, it merely tells you what you did wrong when you use C#. Use C++ if you favor an undiagnosable crash that nobody can help you with.
    – Hans Passant
    Nov 21 at 23:59










  • Thanks for the reply. Yes i understand... jeez.. common sense. I mean anything that happens during the operation will it cause the written file to be corrupted? or partially written
    – user1034912
    Nov 22 at 0:03






  • 3




    The file will be corru
    – Hans Passant
    Nov 22 at 0:10






  • 1




    That was a lot funnier then I intended it to be, sorry. My wife give me funny looks. I ought to be more helpful: use transactional saves. First save to a file with a temporary name. If nothing went wrong then rename it to the file it should be. The File.Replace() overload with three arguments is important to get this right.
    – Hans Passant
    Nov 22 at 0:21












  • There should be League Tables of comments of the day, i think that would win for today
    – TheGeneral
    Nov 22 at 0:24










6




6




The CPU never crashes, it merely tells you what you did wrong when you use C#. Use C++ if you favor an undiagnosable crash that nobody can help you with.
– Hans Passant
Nov 21 at 23:59




The CPU never crashes, it merely tells you what you did wrong when you use C#. Use C++ if you favor an undiagnosable crash that nobody can help you with.
– Hans Passant
Nov 21 at 23:59












Thanks for the reply. Yes i understand... jeez.. common sense. I mean anything that happens during the operation will it cause the written file to be corrupted? or partially written
– user1034912
Nov 22 at 0:03




Thanks for the reply. Yes i understand... jeez.. common sense. I mean anything that happens during the operation will it cause the written file to be corrupted? or partially written
– user1034912
Nov 22 at 0:03




3




3




The file will be corru
– Hans Passant
Nov 22 at 0:10




The file will be corru
– Hans Passant
Nov 22 at 0:10




1




1




That was a lot funnier then I intended it to be, sorry. My wife give me funny looks. I ought to be more helpful: use transactional saves. First save to a file with a temporary name. If nothing went wrong then rename it to the file it should be. The File.Replace() overload with three arguments is important to get this right.
– Hans Passant
Nov 22 at 0:21






That was a lot funnier then I intended it to be, sorry. My wife give me funny looks. I ought to be more helpful: use transactional saves. First save to a file with a temporary name. If nothing went wrong then rename it to the file it should be. The File.Replace() overload with three arguments is important to get this right.
– Hans Passant
Nov 22 at 0:21














There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 at 0:24






There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 at 0:24














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted











  1. XmlSerializer works on a stream


  2. That stream in this case is a FileStream


  3. A FileStream works on a flushable Buffer (of default 4k), meaning when the buffer reaches its max it will flush it to disk



If application crashes or serialisation error happens during serialisation you are likely to have no (or only partial data) flushed to disk



Also Atomic is debatably the wrong word to use here, either way it definitely is'nt Atomic






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422097%2fis-c-sharp-net-serializer-atomic%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted











    1. XmlSerializer works on a stream


    2. That stream in this case is a FileStream


    3. A FileStream works on a flushable Buffer (of default 4k), meaning when the buffer reaches its max it will flush it to disk



    If application crashes or serialisation error happens during serialisation you are likely to have no (or only partial data) flushed to disk



    Also Atomic is debatably the wrong word to use here, either way it definitely is'nt Atomic






    share|improve this answer

























      up vote
      2
      down vote



      accepted











      1. XmlSerializer works on a stream


      2. That stream in this case is a FileStream


      3. A FileStream works on a flushable Buffer (of default 4k), meaning when the buffer reaches its max it will flush it to disk



      If application crashes or serialisation error happens during serialisation you are likely to have no (or only partial data) flushed to disk



      Also Atomic is debatably the wrong word to use here, either way it definitely is'nt Atomic






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted







        1. XmlSerializer works on a stream


        2. That stream in this case is a FileStream


        3. A FileStream works on a flushable Buffer (of default 4k), meaning when the buffer reaches its max it will flush it to disk



        If application crashes or serialisation error happens during serialisation you are likely to have no (or only partial data) flushed to disk



        Also Atomic is debatably the wrong word to use here, either way it definitely is'nt Atomic






        share|improve this answer













        1. XmlSerializer works on a stream


        2. That stream in this case is a FileStream


        3. A FileStream works on a flushable Buffer (of default 4k), meaning when the buffer reaches its max it will flush it to disk



        If application crashes or serialisation error happens during serialisation you are likely to have no (or only partial data) flushed to disk



        Also Atomic is debatably the wrong word to use here, either way it definitely is'nt Atomic







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 0:20









        TheGeneral

        25.9k53162




        25.9k53162






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422097%2fis-c-sharp-net-serializer-atomic%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo