3

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 :)

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It isn't correct, you do can overwrite the element's name using the attribute [XmlArrayItem]
You cannot override the element name dynamically using the XmlSerializer as is. You can statically define it, but this case calls for a name that's based on the data value. Please revise or delete your comment, it's inaccurate.
0

You can use both the [XmlArray] and [XmlArrayItem] and attribute, as it is said here http://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.71).aspx

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

However, I would suggest you to serialize arroung the root level:

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

So you will have a main class with the root and the list and another class for the element:

[Serializable, XmlRoot("Root")]
public class Fields
{

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

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

    [XmlText]
    public string Value;

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

2 Comments

This does not produce the OP's intended serialized output.
The OP is looking to have each <Field> element have a tag name besides "Field" based on the property values of each item in the list. This method does not produce the output the OP is asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.