0

I have the following class that I want to serialize to an XML file

public class HDLCParameters
{
    [XmlElement("ClientMACAddress")]
    public string ClientMACAddress { get; set; }
}

this is serialized like this:

 <HDLCParameters>
     <ClientMACAddress>0x10</ClientMACAddress>
 </HDLCParameters>

but i need it like this:

<HDLCParameters>
     <ClientMACAddress value="0x10" />
</HDLCParameters>

Do i need to create a ClientMACAddress class with a "value" field? isn't there a way to set an element and an attribute with its value?

thanks

1 Answer 1

1

Create the ClientMACAddress class, give it a value field, and specify that it's an attribute (XmlAttributeAttribute)

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

2 Comments

I've just posted a small example from the class. the original class has lots fields just like ClientMACAddress, i would need to create lots of classes.
Create a complex type which has a value attribute. Then use elements like <xs:element name="ClientMACAddress" type="valueAttrType"/>. Then, in your code, all these value instances will be defined in the same class, and you can write helper functions for it.

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.