将XML中的元素集合反序列化为List <t>

时间:2017-02-08 19:33:10

标签: c# xml restsharp

我正在使用RestSharp并尝试将节点集合反序列化为类中的属性:

client.Execute<Foo>(req);

,其中

public class Foo 
{
    public List<Bar> Bar{get;set;}
    ...
}
public class Bar
{
    public int Id{get;set;}
    public string Name{get;set;}
}

并且xml响应是:

<Foo>
    <Bar><Id>4</Id><Name>asdf4</Name></Bar>
    <Bar><Id>5</Id><Name>asdf5</Name></Bar>
    <Bar><Id>6</Id><Name>asdf6</Name></Bar>
</Foo>

但是,该属性总是有一个Bar元素,所有字段都设置为null。我已经研究了所有答案,并根据需要使用尽可能少的课程。这是不可能的。我需要做些什么才能将此响应格式化为列表?我已经尝试过根据几年前的一个问题明确地向DotNet反序列化器指定xml反序列化器,并且没有任何乐趣。

1 个答案:

答案 0 :(得分:0)

要获得所需的C:\Users\Admin\Desktop\deployment>xmlaccess -in C:\Users\Admin\Desktop\deploymen t\ExportAllPortlets.xml -url http://172.16.100.227:10039/wps/config -out Exporte dWebModules.xml -user ****** -password ****** Licensed Materials - Property of IBM, 5724-E76, 5724-E77, 5724-I29 and 5655-Y16, (C) Copyright IBM Corp. 2001, 2014 - All Rights reserved. US Government Users R estricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. EJPXB0001I: Command line parameters: -in <xml input file> [-user <user name>] Will be queried over the console, if omitted [-password <password>] Will be queried over the console, if omitted [-useEncryptedCredentials <file>] Retrieve user name and password from properties file. Update fil e with encrypted password. [-noUpdateProperties] Do not save encrypted password back to properties file. [-out <output file>] default: write to stdout [-url <portal config URL>] default: http://localhost/wps/config [-attempts <max. connection attempts>] default: 1 attempt, no retries [-truststore <file name of the trust store for HTTPS>] default: $JAVA_HOME/lib/security/cacerts [-trustpwd <password for the trust store for HTTPS>] default: <empty> [-trusttype <file type of the trust store for HTTPS>] default: jks [-keystore <file name of the key store for HTTPS>] default: $JAVA_HOME/lib/security/cacerts [-keypwd <password for the key store for HTTPS>] default: <empty> [-keytype <file type of the key store for HTTPS>] default: jks [-credentialexport] enables export and import of credential secrets [-protocol <protocol>] selects the protocol (if portal config URL specified https:, otherwise this parameter is ignored). default: SSL [-encryptionPassphrase <passphrase>] passphrase for credential encryption and decryption ,您只需将List<Bar>更改为:

Execute<T>

修改

根据您的评论,client.Execute<List<Bar>>(req); 还有其他属性。所以你的课可能看起来像这样:

Foo

我认为你可能需要更改你的xml,如果可能的话,更像是这样:

public class Foo 
{
    public List<Bar> Bar {get;set;}
    public string OtherProperty { get; set; }
}

然后将<Foo> <Bars> <Bar><Id>4</Id><Name>asdf4</Name></Bar> <Bar><Id>5</Id><Name>asdf5</Name></Bar> <Bar><Id>6</Id><Name>asdf6</Name></Bar> </Bars> <OtherProp>this</OtherProp> </Foo> 更改为:

Foo

如果您能够更改xml结构并更改该类,则public List<Bar> Bars {get;set;} 应该为您提供所需内容。