c# - Deserialize xml element attributes -


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

question have is, how access uri in 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 set in class deserialize it?

my class right 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     {         { return _name; }         set { _name = value; }     }     [xmlelement(elementname = "edit-version")]     public string editversion     {         { return _editversion; }         set { _editversion = value; }     }     [xmlelement(elementname = "address-type")]     public string addresstype     {         { return _addresstype; }         set { _addresstype = value; }     }     [xmlelement(elementname = "ip-address")]     public string ipaddress     {         { return _ipaddress; }         set { _ipaddress = value; }     }     [xmlelement(elementname = "description")]     public string description     {         { return _description; }         set { _description = value; }     }     [xmlelement(elementname = "host-name")]     public string hostname     {         { return _hostname; }         set { _hostname = value; }     } } 

thanks in advance!

use xmlattributeattribute attribute*

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

if want have serialize system.uri, you'll have separate property uri non-serializable.

[xmlattribute("uri")] public string serializeduri {          {         return uri.tostring();     }     set     {         uri = new uri(value, urikind.relativeorabsolute);     } }  [xmlignore] public uri uri { get; set; } 

with usage, in-code read/write directly uri uri property , ignore serializeduri property. when passed through serializer ignore property , instead use serializedproperty in turn manually serialize/deserialize uri uri property.

* (say 3 times fast)


Comments