繁体   English   中英

反序列化xml元素属性

[英]Deserialize xml element attributes

我承认我在这里学习,并且在序列化和反序列化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; }
    }
}

提前致谢!

使用XmlAttributeAttribute属性*

[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属性进行序列化/反序列化。

*(比方说快3倍)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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