Is C# .NET Serializer Atomic?
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
|
show 1 more comment
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
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 '18 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 '18 at 0:03
3
The file will be corru
– Hans Passant
Nov 22 '18 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 '18 at 0:21
There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 '18 at 0:24
|
show 1 more comment
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
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
c# .net serialization
asked Nov 21 '18 at 23:56
user1034912user1034912
74122336
74122336
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 '18 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 '18 at 0:03
3
The file will be corru
– Hans Passant
Nov 22 '18 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 '18 at 0:21
There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 '18 at 0:24
|
show 1 more comment
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 '18 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 '18 at 0:03
3
The file will be corru
– Hans Passant
Nov 22 '18 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 '18 at 0:21
There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 0:03
3
3
The file will be corru
– Hans Passant
Nov 22 '18 at 0:10
The file will be corru
– Hans Passant
Nov 22 '18 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 '18 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 '18 at 0:21
There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 '18 at 0:24
There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 '18 at 0:24
|
show 1 more comment
1 Answer
1
active
oldest
votes
XmlSerializer
works on a streamThat stream in this case is a
FileStream
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
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
XmlSerializer
works on a streamThat stream in this case is a
FileStream
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
add a comment |
XmlSerializer
works on a streamThat stream in this case is a
FileStream
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
add a comment |
XmlSerializer
works on a streamThat stream in this case is a
FileStream
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
XmlSerializer
works on a streamThat stream in this case is a
FileStream
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
answered Nov 22 '18 at 0:20
TheGeneralTheGeneral
28.8k63365
28.8k63365
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 '18 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 '18 at 0:03
3
The file will be corru
– Hans Passant
Nov 22 '18 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 '18 at 0:21
There should be League Tables of comments of the day, i think that would win for today
– TheGeneral
Nov 22 '18 at 0:24