3

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);
            }
        }
    }
}
Community
  • 1
  • 1
edelwater
  • 2,650
  • 8
  • 39
  • 67
  • well the example that you referenced shoes a .config file.. where are you getting the Status read from – MethodMan Aug 26 '14 at 21:29
  • the status field in the object can be set by many methods in the pipeline. At the end I xmlserializer.Serialize(writer,value) it – edelwater Aug 26 '14 at 21:31
  • hard to determine what's going on when all we see is that public bool Status..can you show the actual code where you are Serializing that object – MethodMan Aug 26 '14 at 21:34
  • 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. – Reg Edit Aug 26 '14 at 21:39
  • @Reg Edit: well.. I have the debugger open the line after I run deserialize, the text XML contains "true" and I see in the debugger that the Status field = false, while all other fields are set. – edelwater Aug 26 '14 at 21:43
  • 1
    That's what I mean--something else is wrong--because that's not what normally happens, is it! (Try it with a separate little noddy app.) What is this string? How is it stored (both before and after serialization/deserializaton)? Perhaps its encoding is not as expected? – Reg Edit Aug 26 '14 at 21:54
  • @Reg Edit you make me depressed lol BUT could be that some other property in the same object plays with the status... and sets it later depending on conditions. – edelwater Aug 26 '14 at 22:19
  • Check. One of the other properties set the status field to false based on its value. Nice. Add Answer and I will V it. – edelwater Aug 26 '14 at 22:39

2 Answers2

3

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.

Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
2

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.)

Reg Edit
  • 6,719
  • 1
  • 35
  • 46