Deserialize objectstring convert string to bool

I have a class, an object that I am serializing to an XML string. no problems.

Demining also works, but it sets a field to be "true" for XML, possibly because it cannot be converted to boolean true.

So I decorated this property

public class X 
{
    // ...
    private bool _status = false;
    [XmlText]
    public bool Status {
        get {return _status;}
        set {_status=value;}
    }
    // ...
}

      

However, then I get "xmlserializer" - there was an error reflecting type X "...

So what's the workaround other than replacing all of my checks with the Status string instead?

ref: - XmlSerializer - an error occurred reflecting the type - Deserialize boolean element with string attribute

update on demand: serialize / deserialize class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// for serializer:
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace StackOverflow
{

    static class Serializing
    {
        public static T XmlDeserializeFromString<T>(this string objectData)
        {
            return (T)XmlDeserializeFromString(objectData, typeof(T));
        }

        public static object XmlDeserializeFromString(this string objectData, Type type)
        {
            var serializer = new XmlSerializer(type);
            object result;

            using (TextReader reader = new StringReader(objectData))
            {
                result = serializer.Deserialize(reader);
            }

            return result;
        }

        public static string Serialize<T>(this T value)
        {
            if (value == null) { return string.Empty; }
            try
            {
                var xmlserializer = new XmlSerializer(typeof(User));
                var stringWriter = new StringWriter();
                using (var writer = XmlWriter.Create(stringWriter))
                {
                    xmlserializer.Serialize(writer, value);
                    return stringWriter.ToString();
                }
            }
            catch (Exception e)
            {
                throw new Exception("A Serialization Error Occurred", e);
            }
        }
    }
}

      

+3


source to share


2 answers


Coming back to your question, Deserializing also works but it sets a field that is the XML "true" to false (probably because it can not convert to boolean true.

perhaps you are too willing to accept this earlier problem? Something else is wrong I suspect.



I say something is wrong because that's not what usually happens, is it? (Try with a little nod.)

+2


source


An exception was thrown because of an attribute [XmlText]

that does not work with the type bool

. But if you remove the XmlText attribute it should work fine.

Another exception I think is related to the type User

in your Serialize method. You have to change this to T to make it correct, as this is a generic method:

public static string Serialize<T>(this T value)
{
    if (value == null) { return string.Empty; }
    try
    {
        var xmlserializer = new XmlSerializer(typeof(T));
        var stringWriter = new StringWriter();
        using (var writer = XmlWriter.Create(stringWriter))
        {
            xmlserializer.Serialize(writer, value);
            return stringWriter.ToString();
        }
    }
    catch (Exception e)
    {
        throw new Exception("An Error Occurred", e);
    }
}

      

You can also convert your property to auto property.

Because this code:



private bool _status = false;

public bool Status
{
    get { return _status; }
    set { _status = value; }
}

      

Is equal to:

public bool Status { get; set; }

      

Since bool is a value type that defaults to false.

+3


source







All Articles