我确实在这里学习,并在序列化和反序列化xml方面取得了进展。
我的问题是,如何访问以下XML中的URI
?
<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>
真的不确定如何在我的班级中设置它以反序列化它?
我的班级现在看起来像:
[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; }
}
}
提前致谢!
答案 0 :(得分:2)
[XmlAttribute]
public string uri
{
get { return _uri; }
set { _uri = value; }
}
如果您希望将其序列化为System.Uri
,则必须使用单独的属性来执行此操作,因为Uri
是不可序列化的。
[XmlAttribute("uri")]
public string SerializedUri
{
get
{
return uri.ToString();
}
set
{
uri = new Uri(value, UriKind.RelativeOrAbsolute);
}
}
[XmlIgnore]
public Uri uri { get; set; }
通过这种用法,您可以在代码中直接读/写Uri uri
属性并忽略SerializedUri
属性。当通过序列化程序时,它将忽略该属性,而是使用SerializedProperty
,而Uri uri
将手动序列化/反序列化{{1}}属性。
*(说快3倍)