c# - How to serialize an integer element with no value? -
i have xml element looks this:
<framerate_denominator nil="true"/> my class member decorated this:
[xmlelement("framerate_denominator")] public int? frameratedenominator; public bool shouldserializeframeratedenominator() { return frameratedenominator.hasvalue; } my code follows:
using serialization; using system.io; using system.xml.serialization; namespace serializationsandbox { class program { static void main(string[] args) { // read job job = null; string path = @"c:\sample_job.xml"; xmlserializer xmlserializer = new xmlserializer(typeof(job)); using (streamreader reader = new streamreader(path)) { //exception here job = (job)xmlserializer.deserialize(reader); reader.close(); } // write xmlserializer = new xmlserializer(job.gettype()); using (stringwriter textwriter = new stringwriter()) { xmlserializer.serialize(textwriter, job); var text = textwriter.tostring(); } } } } i'm getting following exception when deserialize xml:
formatexception: input string not in correct format.
i'm not entirely sure how can handle these elements nil="true"attribute value. have advice?
thanks in advance...
as check similar questions , answer can suggest change frameratedenominator of job class :
[xmlelement("framerate_denominator")] public string _frameratedenominator { get; set; } [xmlignore] public int? frameratedenominator => !string.isnullorempty(_frameratedenominator) ? (int?) int.parse(_frameratedenominator) : null;
Comments
Post a Comment