c# – Serialize Property as Xml Attribute in Element

c# – Serialize Property as Xml Attribute in Element

You will need wrapper classes:

public class SomeIntInfo
{
    [XmlAttribute]
    public int Value { get; set; }
}

public class SomeStringInfo
{
    [XmlAttribute]
    public string Value { get; set; }
}

public class SomeModel
{
    [XmlElement(SomeStringElementName)]
    public SomeStringInfo SomeString { get; set; }

    [XmlElement(SomeInfoElementName)]
    public SomeIntInfo SomeInfo { get; set; }
}

or a more generic approach if you prefer:

public class SomeInfo<T>
{
    [XmlAttribute]
    public T Value { get; set; }
}

public class SomeModel
{
    [XmlElement(SomeStringElementName)]
    public SomeInfo<string> SomeString { get; set; }

    [XmlElement(SomeInfoElementName)]
    public SomeInfo<int> SomeInfo { get; set; }
}

And then:

class Program
{
    static void Main()
    {
        var model = new SomeModel
        {
            SomeString = new SomeInfo<string> { Value = testData },
            SomeInfo = new SomeInfo<int> { Value = 5 }
        };
        var serializer = new XmlSerializer(model.GetType());
        serializer.Serialize(Console.Out, model);
    }
}

will produce:

<?xml version=1.0 encoding=ibm850?>
<SomeModel xmlns_xsi=http://www.w3.org/2001/XMLSchema-instance xmlns_xsd=http://www.w3.org/2001/XMLSchema>
  <SomeStringElementName Value=testData />
  <SomeInfoElementName Value=5 />
</SomeModel>

Kind of, use the XmlAttribute instead of XmlElement, but it wont look like what you want. It will look like the following:

<SomeModel SomeStringElementName=testData> 
</SomeModel> 

The only way I can think of to achieve what you want (natively) would be to have properties pointing to objects named SomeStringElementName and SomeInfoElementName where the class contained a single getter named value. You could take this one step further and use DataContractSerializer so that the wrapper classes can be private. XmlSerializer wont read private properties.

// TODO: make the class generic so that an int or string can be used.
[Serializable]  
public class SerializationClass
{
    public SerializationClass(string value)
    {
        this.Value = value;
    }

    [XmlAttribute(value)]
    public string Value { get; }
}


[Serializable]                     
public class SomeModel                     
{                     
    [XmlIgnore]                     
    public string SomeString { get; set; }                     

    [XmlIgnore]                      
    public int SomeInfo { get; set; }  

    [XmlElement]
    public SerializationClass SomeStringElementName
    {
        get { return new SerializationClass(this.SomeString); }
    }               
}

c# – Serialize Property as Xml Attribute in Element

Leave a Reply

Your email address will not be published.