C # Xml Serialization Inline Elements

Is there a way to get an XML serializer to add an element as an inline, rather than using the Value layout. I basically only have a giant list of structures, and I would like to add an inline element for each helper element that is included.

<main>
<item>
  <value>1</value>
  <name>Alphabet</name>
</item>
...
</main>

Basically I want to add:

<item Enabled="true">

if the block of elements is activated. Is there any way to do this?

+3
source share
2 answers

Yes, just check the Enabled property with XmlAttributeAttribute:

[XmlAttribute("Enabled")]
public bool Enabled { get; set; }

Documentation of the attributes that control XML serialization can be found on MSDN: http://msdn.microsoft.com/en-us/library/83y7df3e%28v=VS.100%29.aspx

+2
source

XmlAttributeAttribute

XmlAttributeAttribute , . , . Collapse

using System;
using System.Xml.Serialization;

namespace XmlEntities {
    [XmlRoot("XmlDocRoot")]
    public class RootClass {
        private int attribute_id;

        [XmlAttribute("id")]
        public int Id {
            get { return attribute_id; }
            set { attribute_id = value; }
        }
    }
}

- ... Collapse

<XmlDocRoot id="1" />

SO: # XML Serialization

+1

All Articles