How to add attributes for C# XML Serialization

c#, xml-serialization

Solution

Where do you have the `type` stored?

Normally you could have something like:

class Document {
    [XmlAttribute("type")]
    public string Type { get; set; }
    [XmlText]
    public string Name { get; set; }
}


public class _Filter    
{
    [XmlElement("Times")]    
    public _Times Times;    
    [XmlElement("Document")]    
    public Document Document;    
}

Problem

I am having an issue with serializing and object, I can get it to create all the correct outputs except for where i have an Element that needs a value and an attribute. Here is the required output: ``` <Root> <Method>Retrieve</Method> <Options> <Filter> <Times> <TimeFrom>2009-06-17</TimeFrom> </Times> <Document type="word">document name</Document> </Filter> </Options> </AdCourierAPI> ``` I can build all of it but can not find a way to set the Document type attribute, here is a segment of the object class ``` [XmlRoot("Root"), Serializable] public class Root { [XmlElement("Method")] public string method="RetrieveApplications"; [XmlElement("Options")] public _Options Options; } public class _Options { [XmlElement("Filter")] public _Filter Filter; } public class _Filter { [XmlElement("Times")] public _Times Times; [XmlElement("Documents")] public string Documents; } ``` which gives me: ``` <Document>document name</Document> ``` rather than: ``` <Document type="word">document name</Document> ``` but I can not find a way to correct this, please advise. Thanks

Original source