Reputation: 23
I am new to XML serialization in C#. I want to serialize my Dependency
object instance so that it looks like this:
<Dependency Software="Some software">some value</Dependency>
I tried this:
public class Dependency{
[XmlAttribute("Software")]
public string soft;
public string value;
}
However, the output looks like the below which is not what I want:
<Dependency Software="Some Software">
<value>some value</value>
</Dependency>
Is there any way to achieve my desired output?
Upvotes: 1
Views: 81
Reputation: 5812
A public property's value will appear in an element with the property name if you do not tell the serializer otherwise.
To get the output you want, you need to decorate it with the XmlText
attribute, for example:
public class Dependency
{
[XmlAttribute("Software")]
public string soft;
[XmlText]
public string value;
}
The property value will then appear as the value of the parent class element - Dependency
in this case.
Upvotes: 3