English 中文(简体)
WCF:XmlSerialization Depende被忽视
原标题:WCF: XmlSerialization attribute ignored for the root element

I m从事一项REStful WCF服务,该服务由XMLSerializer(而不是DataContract序列izer)生成。

尽管大多数物体的编排格式正确,但所归还物品的根本内容似乎忽略了我的XML序列化特性。

例如,资源<代码>/账户/回收了我的会计类别(自己是我自己的<代码>的子类)的XML序列化代表。 ObjectList<T> category, which has some property on it should be seriesized. 然而,我不想获得我想要的结果。

我的守则是:

[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")
    };
}

这就是网络服务的回报:

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

因此,主要问题是,我所期望的会计类别上的序列被忽略,我也想知道,如何将其“Account”作为“名称”一类的下级财产(I used [XmlElement(“名称”)是对这些财产使用的,并且是有效的。

感谢!

问题回答

不能百分之百确定这项工作,但试图在方法中增加以下特性:

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

最新情况:

由于[返回:*],上述工作不可行。 特性没有被放弃。 两项行之有效的选择:

您可将会计科目列入清单,并使用[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; }
}




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签