« Ashcroft: mission accomplished | Main | Property helpstrings in .NET/COM interop »

November 11, 2004

XML shaping in .NET

Finally lost patience with the MSDN documentation on XML shaping attributes.  Lots of description of how to construct the attributes and apply them and inject them and so on, but not a single illustration of what they actually do to the XML!  I found I kept writing little test harnesses to check whether, for a given XML format, I needed XmlArrayAttribute or XmlElementAttribute or XmlTattooItOnYourDogAttribute or... anyway, I'm now doing what I should have done from the start, capture the effects systematically and put the document somewhere I can find it again.

The Sample Type Definition

public class Widget
{
   private Grommit[] _grommits;

   public Grommit[] Grommits
   {
      get { return _grommits; }
      set { _grommits = value; }
   }
}

public class Grommit
{
   private string _thread;

   public string Thread
   {
      get { return _thread; }
      set { _thread = value; }
   }
}

Default Serialisation

Default behaviour:

  • The array maps to an element with the same name as the property (Grommits)
  • The members of the array map to child elements of this, with the name of the type (Grommit)
<Widget1>
  <Grommits>
    <Grommit>
      <Thread>Left</Thread>
    </Grommit>
    <Grommit>
      <Thread>Right</Thread>
    </Grommit>
  </Grommits>
</Widget1>

Customising the Name of the Array Container Element: XmlArrayAttribute

If you apply XmlArrayAttribute to the Grommits property:

  • The array maps to an element with the name specified in the attribute
  • The members of the array map to child elements of this, with the name of the type (Grommit)
<Widget2>
  <GrommitList>  <----------------------- This element name has changed
    <Grommit>
      <Thread>Left</Thread>
    </Grommit>
    <Grommit>
      <Thread>Right</Thread>
    </Grommit>
  </GrommitList>
</Widget2>

Customising the Name of the Contained Elements: XmlArrayItemAttribute

If you apply XmlArrayItemAttribute to the Grommits property:

  • The array maps to an element with the same name as the property (Grommits)
  • The members of the array map to child elements of this, with the name specified in the attribute
<Widget5>
  <Grommits>
    <JustOneGrommit>  <-------------------- This element name has changed
      <Thread>Left</Thread>
    </JustOneGrommit>
    <JustOneGrommit>
      <Thread>Right</Thread>
    </JustOneGrommit>
  </Grommits>
</Widget5>

Elements Without a Container: XmlElementAttribute

If you apply the XmlElementAttribute to the Grommits property:

  • There is no container element -- no XML tag corresponding to the Grommits property
  • The members of the array map to child elements of the root, with the name specified in the attribute
<Widget3>
  <JustOneGrommit>         <--- No grouping element around grommits, direct child of Widget3
    <Thread>Left</Thread>
  </JustOneGrommit>
  <JustOneGrommit>
    <Thread>Right</Thread>
  </JustOneGrommit>
</Widget3>

Obviously these can be combined where appropriate.

November 11, 2004 in Software | Permalink

TrackBack

TrackBack URL for this entry:
https://www.typepad.com/services/trackback/6a00d8341c5c9b53ef00d8350aa0a553ef

Listed below are links to weblogs that reference XML shaping in .NET:

Comments