Reputation: 611
I'm serializing a structure that results in this output:
<NachrichtenKonfiguration>
<Elemente>
<Element>
<Typ>Bool</Typ>
<Bezeichnung>Gut</Bezeichnung>
</Element>
<Element>
<Typ>Int</Typ>
<Bezeichnung>Dauer</Bezeichnung>
</Element>
</Elemente>
<Name>Teiledaten</Name>
</NachrichtenKonfiguration>
And I would like it to be rather like this:
<NachrichtenKonfiguration Name="Teiledaten">
<Elemente>
<Element Typ="Bool" Bezeichnung="Gut"/>
<Element Typ="Int" Bezeichnung="Schleifdauer"/>
</Elemente>
</NachrichtenKonfiguration>
Is it possible to make XmlSerialzer / XmlWriter do that (Use attributes instead of nested elements)?
Greetings,
Tim
Upvotes: 0
Views: 228
Reputation: 611
Ok I got it, you simply need to add the [XmlAttribute]-tag over the corresponding declaration.
Here's how. If you have a class called "Person" and you have two attributes in it, write the code like this:
[Serializable]
public class Person
{
[XmlAttribute]
public int Age;
[XmlAttribute]
public string Name;
public Person()
{
}
}
When serializing (with XmlWriter settings set to indent lines) above structure results in this xml code:
<Person Age="21" Name="Stacky" />
Upvotes: 2