views:

229

answers:

1

Warning -- I'm not an xml guru.

Here is what I have:

<Fields>
  <Field name="BusinessName" look-up="true">My Business</Field>
  <Field name="BusinessType" look-up="false">Nobody really knows!</Field>
</Fields>

This maps back to:

[XmlArrayItem(ElementName = "Field")]
public List<UserInfoField> Fields;

and

[Serializable, XmlRoot("Field")]
public class UserInfoField
{
    [XmlAttributeAttribute("name")]
    public string Name;

    [XmlText]
    public string Value;

    [XmlAttributeAttribute("look-up")]
    public bool LookUp;
}

Is there anyway to get this serialize output instead:

<Fields>
  <BusinessName look-up="true">My Business</BusinessName>
  <BusinessType look-up="false">Nobody really knows!</BusinessType>
</Fields>

I understand that this might be overly magical and can imagine there is a good reason this shouldn't work ... but I figure it might and this is a good place to ask :)

+4  A: 

The XmlSerializer (well, all Framework serializers actually) natively serialize types, not names. The attribution decorators let you get in front of that a bit with names, but those are static run-time lookups, so they don't permit you to interject into the serialization process using that structure.

Instead, what you want to do is write your own serialization routine. This will permit you to override the node naming sequence you want -- essentially interjecting the property of the Name field as your node name. You're interested in implementing the IXmlSerializable interface. Keep in mind this has consequences in dealing with deserialization as well.

jro