2011-08-03 119 views
1

我的工作,它返回XML序列生成的(而不是在DataContract串行)XML REST风格的WCF服务XmlSerialization属性。WCF:忽略根元素

虽然大多数对象的格式是否正确,是什么返回根元素似乎被忽略我的XML序列化的属性。

例如,资源/accounts/返回我的AccountList类(本身是我自己的ObjectList<T>类的子类,它有一些应该序列化的属性)的XML序列化表示。但是我没有得到我想要的结果。

这里是我的代码:

[XmlRoot("accounts")] 
public class AccountList : ObjectList<Account> { 
} 

public class ObjectList<T> : List<T> { 
    [XmlAttribute("foo")] 
    public Int32 FooProperty { get; set; } 
} 

[OperationContract] 
[WebGet(UriTemplate="/accounts")] 
public AccountList GetAccounts() { 
    return new AccountList() { 
     new Account("bilbo baggins"), 
     new Account("steve ballmer") 
    }; 
} 

而且这是由Web服务返回:

<arrayOfAccount> 
    <Account> 
     <name>biblo baggins</name> 
    </Account> 
    <Account> 
     <name>steve ballmer</name> 
    </Account> 
</arrayOfAccount> 

所以,主要的问题是,在为accountList类我想要的序列被忽略,我也想知道如何得到它,所以“帐户”是小写的,就像“名称”属性(我在这些属性上使用了[XmlElement(“name”)],并且它可以正常工作。 10谢谢!

回答

0

不是100%肯定这会工作,但尝试添加下列属性的方法:

[return:XmlArray("accounts")] 
[return:XmlArrayItem("account")] 

更新:

以上不因工作[返回:*]属性没有得到已接。两个选项做的工作:

你可以为accountList包含一个列表,并使用[XmlElement的(“账户”)那里,像这样:

[XmlRoot("accounts")] 
public class AccountList : ObjectList<Account> { 
    [XmlElement("account")] 
    public List<Account> Accounts { get; set; } 
} 

public class ObjectList<T> {//: List<T> { 
    [XmlAttribute("foo")] 
    public Int32 FooProperty { get; set; } 
} 

另外,如果你不介意改变响应xml,你可以添加一个包装类,并使用前面描述的[XmlArray]和[XmlarrayItem]:

[XmlRoot("response")] 
public class GetAccountResponse { 
    [XmlArray("accounts"), XmlArrayItem("account")] 
    public AccountList Accounts { get; set; } 
} 
+0

不,根本没有用,对不起。 – Dai

+0

是的,似乎[return:*]属性没有被拾起,太糟糕了。 – alexdej