Serializing XML in C #

I have a class that looks like this:

class SomeClass
    {
        private int _property1;
        [XmlAttribute("Property1")]
        public int Property1
        {
            get { return _property1; }
            set { _property1 = value; }
        }
        private int _property2;
        [XmlAttribute("Property2")]
        public int Property2
        {
            get { return _property2; }
            set { _property2 = value; }
        }

        private string _property3;
        public string Property3
        {
            get { return _property3; }
            set { _property3 = value; }
        }

        public SomeClass()
        {
        }
    }

I need to serialize it using XmlSerializer in the following format:

<SomeClass Property1="NNNNN" Property2="NNNNN">
     Value_of_Property3
</SomeClass>

However, I cannot figure out how I can serialize the value of Property3 without adding a node for Property3. Do I need to serialize a string in Property3 without adding a node for this?

+3
source share
2 answers

Add attribute [XmlText()]to Property3.

+4
source
[XmlText]
public string Property3
{
    get { return _property3; }
    set { _property3 = value; }
}
+2
source

Source: https://habr.com/ru/post/1795284/


All Articles