1

I'm admittedly learning here, and have made progress in serializing and deserialzing xml.

Question that I have is, how do I access the URI in the XML below?

<address href="/api/juniper/sd/address-management/addresses/98677" 
uri="/api/juniper/sd/address-management/addresses/98677">
  <name>Any-IPv4</name> 
  <address-type>ANY_IPV4</address-type>  
  <description>Predefined any-ipv4 address</description> 
  <host-name /> 
  <id>98677</id>  
</address>

Really not sure how to set that up in my class to deserialize it?

My class right now looks like:

[XmlRoot("address", Namespace = "")]
public class address
{
    string _name;
    string _editVersion;
    string _addressType;
    string _ipAddress;
    string _description;
    string _hostName;
    string _zone;
    string _addressVersion;
    string _definitionType;
    string _createdByUserName;
    string _lastModifiedByUser;
    string _createdTime;
    string _lastModifiedTime;
    string _id;

    [XmlElement(ElementName = "name")]
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
    [XmlElement(ElementName = "edit-version")]
    public string editversion
    {
        get { return _editVersion; }
        set { _editVersion = value; }
    }
    [XmlElement(ElementName = "address-type")]
    public string addresstype
    {
        get { return _addressType; }
        set { _addressType = value; }
    }
    [XmlElement(ElementName = "ip-address")]
    public string ipaddress
    {
        get { return _ipAddress; }
        set { _ipAddress = value; }
    }
    [XmlElement(ElementName = "description")]
    public string description
    {
        get { return _description; }
        set { _description = value; }
    }
    [XmlElement(ElementName = "host-name")]
    public string hostname
    {
        get { return _hostName; }
        set { _hostName = value; }
    }
}

Thanks in advance!

Travis Johnson
  • 109
  • 1
  • 9
  • Using the [XmlAttribute](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributeattribute.aspx) attribute. Have a look at [this SO entry](http://stackoverflow.com/questions/11330643/serialize-property-as-xml-attribute-in-element). – keenthinker Jul 15 '13 at 21:30

1 Answers1

2

Use the XmlAttributeAttribute attribute*

[XmlAttribute]
public string uri
{
    get { return _uri; }
    set { _uri = value; }
}

If you want to have it serialize it as System.Uri, you'll have to do it with a separate property as Uri is non-serializable.

[XmlAttribute("uri")]
public string SerializedUri
{ 
    get
    {
        return uri.ToString();
    }
    set
    {
        uri = new Uri(value, UriKind.RelativeOrAbsolute);
    }
}

[XmlIgnore]
public Uri uri { get; set; }

With this usage, in-code you would read/write directly to the Uri uri property and ignore the SerializedUri property. When passed through the serializer it will ignore that property and instead use the SerializedProperty which will in turn manually serialize/deserialize the Uri uri property.

* (say that 3 times fast)

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • Thanks Chris, I'll give that a shot. Is there a way to prevent it from serializing, and only apply during deserialization? – Travis Johnson Jul 15 '13 at 22:04
  • Sorry Travis, I'm not sure if I understand. You mean you want to _read_ the "uri" values from the XML, but if you were to serialize/output XML, you do _not_ want the "uri" attribute written? – Chris Sinclair Jul 15 '13 at 22:07
  • If that's the case, a _quick hack_ would be to have the `SerializedUri` get method return null instead: `public string SerializedUri { get { return null; } set { uri = new Uri(value, UriKind.RelativeOrAbsolute); } }`. The `XmlSerializer` omits any `null` values and won't include the attribute; however the setter will still execute when reading in any existing/legacy uri attributes. – Chris Sinclair Jul 15 '13 at 22:11