Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize objectstring convert string to bool

I have a class, which object I serialize to an XML string. no prob.

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

So I decorated that property with

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 is the workaround other than replace all my checks against a string Status instead?

ref: - XmlSerializer - There was an error reflecting type - Deserialize boolean element with string attribute

update on request: The 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);
            }
        }
    }
}
like image 652
edelwater Avatar asked Dec 03 '25 17:12

edelwater


2 Answers

The exception is thrown because of the [XmlText] attribute, which doesn't work well with the type bool. But if you remove the XmlText attribute it should work fine.

The other exception I think comes from the type User in your Serialize method. You should change that to T to make it correct, as it 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 should also be able to convert your property into a auto property.

Because this code:

private bool _status = false;

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

Is equal to this:

public bool Status { get; set; }

As bool is a value type that by default is set to false.

like image 98
Mikael Engver Avatar answered Dec 05 '25 06:12

Mikael Engver


Going back a step in 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 accept that earlier problem too willingly? Something else wrong I suspect.

I say something else is wrong because that's not what normally happens, is it! (Try it with a separate little noddy app.)

like image 25
Reg Edit Avatar answered Dec 05 '25 05:12

Reg Edit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!