【问题标题】:XmlSerializer remove redundant parent elementXmlSerializer 删除多余的父元素
【发布时间】:2013-10-29 06:16:08
【问题描述】:

我有这样的课;

 public class Orderinfo
            {
                public string billing_company { get; set; }
                public string delivery_company { get; set; }
                public string delivery_phone { get; set; }
                public string orderid { get; set; }
                public string email_address { get; set; }
                public string delivery_firstname { get; set; }
                public string delivery_lastname { get; set; }
                public string delivery_street_address { get; set; }
                public string delivery_city { get; set; }
                public string delivery_state { get; set; }
                public string delivery_postcode { get; set; }
                public string giftwrap_type { get; set; }
                public string billing_firstname { get; set; }
                public string billing_lastname { get; set; }
                public string billing_street_address { get; set; }
                public string billing_city { get; set; }
                public string billing_state { get; set; }
                public string billing_postcode { get; set; }
                public string billing_phone { get; set; }
                public string transaction_id { get; set; }
                public string shippingtype { get; set; }
                public string cc_type { get; set; }
                public string personal_note { get; set; }
            }

            public class Ordertotals
            {
                public string shipping { get; set; }
                public string subtotal { get; set; }
                public string tax { get; set; }
                public string discount { get; set; }
                public string total { get; set; }
            }

            public class Product
            {
                public string product_id { get; set; }
                public string product_sku { get; set; }
                public string product_quantity { get; set; }
                public string product_price { get; set; }
                public string product_name { get; set; }
            }

            public class Orderproducts
            {
                public List<Product> product { get; set; }
            }

            public class Order
            {
                public Orderinfo orderinfo { get; set; }
                public Ordertotals ordertotals { get; set; }
                public Orderproducts orderproducts { get; set; }
            }

当我用下面的函数序列化这个类时;

public class Utf8StringWriter : StringWriter
        {
            public override Encoding Encoding
            {
                get { return Encoding.UTF8; }
            }
        }
        public static string ToXml(object param)
        {
            var stringwriter = new Utf8StringWriter();
            var serializer = new XmlSerializer(param.GetType());
            serializer.Serialize(stringwriter, param);
            return stringwriter.ToString();
    }

我收到以下输出(部分);

<orderproducts>
    <product>
      <Product>
        <product_id>76</product_id>
        <product_sku>16269</product_sku>
        <product_quantity>1</product_quantity>
        <product_price>15.0000</product_price>
        <product_name>Sesame Street Take along Elmo</product_name>
      </Product>
      <Product>
        <product_id>77</product_id>
        <product_sku>16999-1219</product_sku>
        <product_quantity>1</product_quantity>
        <product_price>20.0000</product_price>
        <product_name>Buddy the Dog - Red</product_name>
      </Product>
    </product>
  </orderproducts>

实际上,我希望该产品列表应从 orderproducts 下开始,但它有一个涵盖其他产品类别的额外标签。

有什么方法可以忽略这个标签吗?所以应该是这样的;

<orderproducts>   
      <Product>
        <product_id>76</product_id>
        <product_sku>16269</product_sku>
        <product_quantity>1</product_quantity>
        <product_price>15.0000</product_price>
        <product_name>Sesame Street Take along Elmo</product_name>
      </Product>
      <Product>
        <product_id>77</product_id>
        <product_sku>16999-1219</product_sku>
        <product_quantity>1</product_quantity>
        <product_price>20.0000</product_price>
        <product_name>Buddy the Dog - Red</product_name>
      </Product>   
  </orderproducts>

【问题讨论】:

  • 这只是让真正的问题浮出水面:您的课程Orderproducts 似乎是多余的。您不能将其删除以获得所需的 XML 格式吗?
  • @BartoszKP 我发布 XML 请求的 API 要求 XML 应该是那种格式,产品标签必须被 Orderproducts 父元素覆盖。

标签: c# xml xmlserializer


【解决方案1】:

向 Product 类添加 XmlElement 属性将删除 Products Container。请参阅MSDN 上的这篇文章。

【讨论】:

  • 马克-我们回答了 :) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 2020-10-05
  • 2022-01-22
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多