简体   繁体   中英

XML Attribute as “Complex Property”

I'm not sure if this is possible, but I will try to phrase my question as coherently as possible.

I am learning about ISerielizable and XMLSerializer for objects because I am tired of writing xml. I have the following xml.

<Person>
<FName>John</FName>
<LName>Smith</LName>
<Address Type = "Road">1623 Stagecoach</Address>
</Person>

I have a person Object with the properties of

string FName
string LName
string Address

Instead of adding a property of AddressType = Road,

How can I serilize the Type attribute of the address node to be part of the address property.

The examlple that come to mind is the Font property of Label, or the Size Property.

EDIT:

As I am rereading this, I realize the simple answer is adding a new address class and everything works out cleanly. I guess my question is, is there a method besides that?

Well you might wanna change your class library first and then go for serialization. Try making it:

[XmlRoot("Person")]
public class Person()
{ 
[XmlElement("FName")]  
public string Fname { get; set; }

[XmlElement("LName")]   
public string LName { get; set; }

[XmlElement("Address")]    
public Address Address;   
}

public class Address()
{
[XmlAttribute("Type")]
public string Type { get; set; }

[XmlType]
public string AddrValue { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM