1

我有以下要序列化为 XML 的类结构:

public class Foo
{
    [XmlArray("approxPriceList")]
    [XmlArrayItem("approxPrice")]
    public List<ApproxPriceElement> ApproxPriceList { get; set; }
}

public class ApproxPriceElement
{
    [XmlAttribute("currency")]
    public string Currency { get; set; }

    [XmlElement("approxPrice")]
    public decimal? ApproxPrice { get; set; }
}

如果我序列化Foo,我会得到以下 XML:

<approxPriceList>
    <approxPrice currency="aud">
        <approxPrice>2220.00</approxPrice>
    </approxPrice>
</approxPriceList>

我想要的是以下内容:

<approxPriceList>
    <approxPrice currency="aud">2220.00</approxPrice>
</approxPriceList>

一个想法是变成ApproxPriceList一个FooList<decimal?>但后来我不知道如何将一个currency属性与approxPrice列表中的每个相关联。

有没有办法做到这一点?

4

2 回答 2

1

使用XmlText代替XmlElement("approxPrice")

[XmlText]
public decimal? ApproxPrice { get; set; }

要允许null添加元素:

[XmlArrayItem("approxPrice", IsNullable = true)]
public List<ApproxPriceElement> ApproxPriceList { get; set; }

以下是针对“不能用于编码复杂类型”异常(来源)的建议解决方法:

  [XmlText]
  public decimal ApproxPrice { get; set; }
  [XmlIgnore]
  public bool ApproxPriceSpecified { get { return ApproxPrice >= 0; } }
于 2013-04-22T00:57:50.857 回答
0

你可以使用IXmlSerializable

public class Foo : IXmlSerializable
    {
        public Foo()
        {
            ApproxPriceList = new List<ApproxPriceElement>();
        }

        public List<ApproxPriceElement> ApproxPriceList { get; set; }

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    switch (reader.LocalName)
                    {
                        case "approxPrice":
                            {
                                var approxPrice = new ApproxPriceElement();

                                approxPrice.Currency = reader.GetAttribute("currency");

                                var approxPriceValue = reader.ReadElementContentAsString();

                                if (!string.IsNullOrEmpty(approxPriceValue))
                                {
                                    approxPrice.ApproxPrice = decimal.Parse(approxPriceValue);
                                }

                                ApproxPriceList.Add(approxPrice);
                            }
                            break;
                    }
                }
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("approxPriceList");
            foreach (var approxPrice in ApproxPriceList)
            {
                writer.WriteStartElement("approxPrice");
                writer.WriteAttributeString("currency", approxPrice.Currency);
                writer.WriteString(approxPrice.ApproxPrice.ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
    }

    public class ApproxPriceElement
    {
        public string Currency { get; set; }

        public decimal? ApproxPrice { get; set; }
    }

它不是那么方便,但它可以完成工作。

于 2013-04-22T02:17:55.230 回答