Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

XmlSerializer store SubObject as Attribute

$
0
0

I serialze a List of objects by using

the class looks like this:
public class MyClass { [XmlAttribute] public string FullName { get; set; } [XmlAttribute] public string ShortName { get; set; } [XmlAttribute] public bool HasSpecialTextColor { get; set; } [XmlElement] public xColor SpecialTextColor { get; set; } = Color.Black; [XmlAttribute] public bool HasSpecialBackgroundColor { get; set; } [XmlElement] public xColor SpecialBackgroundColor { get; set; } = Color.Transparent; }

my xColor-Class:
`
public struct xColor
{
[XmlIgnore]
public Color mColor { get; private set; }

    public xColor(Color clr)
    {
        mColor = clr;
    }

    public static implicit operator Color(xColor x)
    {
        return x.mColor;
    }

    public static implicit operator xColor(Color c)
    {
        return new xColor(c);
    }

    [XmlText]
    public string HexString
    {
        get
        {                
            return "#" + ((int)(mColor.A*255)).ToString("X2") + ((int)(mColor.R * 255)).ToString("X2") + ((int)(mColor.G * 255)).ToString("X2") + ((int)(mColor.B * 255)).ToString("X2");
        }
        set
        {
            if (value.StartsWith("#")) {
                double a = Convert.ToInt32(value.Substring(1, 2), 16);
                double r = Convert.ToInt32(value.Substring(3, 2), 16);
                double g = Convert.ToInt32(value.Substring(5, 2), 16);
                double b = Convert.ToInt32(value.Substring(7, 2), 16);
                mColor = new Color(r / 255, g / 255, b / 255, a / 255);
            }
        }
    }
}

`

so I get one XMLElement per myClass that look's like this:
<MyClass FullName="Pinco" ShortName="Po" HasSpecialTextColor="false" HasSpecialBackgroundColor="false"> <SpecialTextColor>#FF000000</SpecialTextColor> <SpecialBackgroundColor>#00FFFFFF</SpecialBackgroundColor> </WeekDays>

and I'd like to get this output:
<MyClass FullName="Pinco" ShortName="Po" HasSpecialTextColor="false" HasSpecialBackgroundColor="false" SpecialTextColor="#FF000000" SpecialBackgroundColor="#00FFFFFF"/>

is this possible by setting this XML-Attributes correctly or do I have to write this serializion-part manually?

and why is this first code-block bad formatted?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>