错误的xml文档(1,2)...不是预期的

时间:2014-06-17 17:24:29

标签: c# xml-serialization

我已尝试过针对同一错误在其他SO答案中找到的一些建议,但没有任何帮助。使用空白命名空间attr添加命名空间attr。将字符串加载到xmlDocument中,然后将其重写回xmlWriter。使用Serialazble代替DataContract。使用带有和不带命名空间的xmlRoot attr。什么都行不通!帮助

我正在尝试使用接受xml格式字符串的构造函数实例化FillRequestExtended,并始终在xml文档中获取错误错误(1,2)

内部异常读取:“http://schemas.datacontract.org/2004/07/WdRx.Exactus.Objects'>不是预期的。”

该课程如下:

namespace WdRx.Exactus.Objects
{

    [DataContract]
    public class FillRequestExtended : IExtendedData
    {

        [DataMember]
        [XmlElement]
        public KeyValuePair<string, string>[] Properties { get; set; }

        [DataMember]
        [XmlElement]
        public List<Payment> PaymentItems { get; set; }

        public FillRequestExtended()
        {
        }

        public FillRequestExtended(string xml)
        {
            FillRequestExtended extendedData;

            XmlSerializer xs = new XmlSerializer(typeof(FillRequestExtended));

            using (StringReader sr = new StringReader(xml))
            {
                XmlReader reader = XmlReader.Create(sr);
                extendedData = (FillRequestExtended)xs.Deserialize(reader);
            }

            Properties = extendedData.Properties;
            PaymentItems = new List<Payment>(extendedData.PaymentItems);
        }

    }

}

传入的字符串在其他地方被序列化,没有任何问题,看起来像这样:

<FillRequestExtended xmlns=\"http://schemas.datacontract.org/2004/07/WdRx.Exactus.Objects\"
                     xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
  <PaymentItems>
    <Payment>
      <Amount>-43.95</Amount>
      <Summary>CCP PAYMENT - AUTH:014910</Summary>
    </Payment>
    <Payment>
      <Amount>0.00</Amount>
      <Summary>Type: VIS   Account: ************5793   Expires: 05/16   Authorization: 014910</Summary>
    </Payment>
  </PaymentItems>
  <Properties xmlns:a=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
    <a:KeyValuePairOfstringstring>
      <a:key>RxDcDate</a:key>
      <a:value>20150414</a:value>
    </a:KeyValuePairOfstringstring>
    <a:KeyValuePairOfstringstring>
      <a:key>RefillStatus</a:key>
      <a:value>PROFILED</a:value>
    </a:KeyValuePairOfstringstring>
  </Properties>
</FillRequestExtended>

1 个答案:

答案 0 :(得分:2)

至少有两种不同的XML反序列化器。通过使用[DataContract]修饰类,您强制它使用WCF序列化程序。在反序列化代码中,您使用的是普通的XML对象反序列化器。

相关问题