Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize char data type with XmlSerializer

I have a class which has property whiches type is char as following

    [XmlRoot("Root")]
    public class TestClass
    {
        [XmlElement("Test", typeof(char))]
        public char TestProperty { get; set; }
    }

When value of TestProperty is 'N' and if I serialize TestClass it will produce following result:

    <Root>
        <Test>78</Test>
    </Root>

But what I want is to have following

    <Root>
        <Test>N</Test>
    </Root>

Is it possible without changing type of TestProperty to string?

like image 309
Adil Mammadov Avatar asked Apr 17 '13 07:04

Adil Mammadov


People also ask

What is XmlSerializer C#?

The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors. If you use any of the constructors other than the one that takes a type then a new temporary assembly is created EVERY TIME you create a serializer, rather than only once.

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.

How can you modify control the XML that is being generated by XmlSerializer?

By applying the XmlRootAttribute, you can control the XML stream generated by the XmlSerializer. For example, you can change the element name and namespace. The XmlTypeAttribute allows you to control the schema of the generated XML.

What is serialize in XML?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.


1 Answers

Not AFAIK. You can cheat, though:

[XmlIgnore]
public char TestProperty { get; set; }

[XmlElement("Test"), Browsable(false)]
public string TestPropertyString {
    get { return TestProperty.ToString(); }
    set { TestProperty = value.Single(); }
}
like image 197
Marc Gravell Avatar answered Oct 03 '22 01:10

Marc Gravell