转换为XML而不显示元素时的c#类

问题描述 投票:0回答:1

我正在尝试使用c#类创建一个XMl请求。

预期的输出是

<UserRequest>
  <action>manage</action>
  <create>
    <name>Companies</name>
    <item>
    <symbol>GOOG</symbol>
    <item />
  </create>
 </UserRequest>

当我使用XMl属性创建如下所示的类时

[XmlRoot("WorkflowRequest")]
    public class UserRequest : RequestBase
    {
        public string action { get; set; }

        [XmlElement("create")]
        public Create create { get; set; }
    }


    [XmlType(AnonymousType = true)]
    public class Create
    {       
        public string name { get; set; }
        [XmlElement("item")]
        public Item item { get; set; }
    }

    [XmlType(AnonymousType = true)]
    public class Item
    {
        public string symbolType;       
        public string symbol;
        public string qty;
        public string purchasePrice;
        public string purchaseCost;
        public string purchaseDate;
        public string putOrCall;
        public string strikePrice;
        public string expirationDate;
    }

Item symbolInfo = new Item();
symbolInfo.symbol = "GOOG";

Create ct = new Create()
{
     name = "Companies",
     item = symbolInfo
};
UserRequest userRequest = new UserRequest()
{
     action = "manage",
     create = ct
 };

我得到一个奇怪的请求与item标签而不是GOOG ..有人可以指导我做错的地方这就是我得到的..

<UserRequest>
  <action>manage</action>
  <create>
    <name>Companies</name>
    <item />
  </create>
 </UserRequest>
c# .net
1个回答
0
投票
[XmlType(AnonymousType = true)]
public class Item
{    
    [XmlElement("symbol")]
    public string symbol;

    //other properties
}
© www.soinside.com 2019 - 2024. All rights reserved.