2011-08-03 117 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