xml序列化错误

时间:2011-06-29 08:01:24

标签: c# asp.net xml-serialization

我正在尝试使用xml序列化序列化一个对象(MCCI_IN000001TR01Message,这是一个使用Web服务的类),但在序列化时我收到错误。

MCCI_IN000001TR01Message message = new MCCI_IN000001TR01Message()
{
   id = new MCCI_IN000001TR01MessageID()
            {
                root = "2.16.840.1.113883.3.129.2.1.2",
                extension = _sGuid
            },

   acceptAckCode =
       new MCCI_IN000001TR01MessageAcceptAckCode()

           {
               code = "AL"
           },
   receiver =
       f_Receiver(),
   sender = f_Sender(),
   controlActEvent = f_ControlActEvent(dialysis)
};

MCCI_IN000002TR01Message donen = service.MCCI_AR000001TR_MCCI_IN000001TR(message);

MCCI_IN000001TR01Message是一个不是由我创建的类。它是Web服务类。

  

在此上下文中不能使用System.String类型。   描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

Exception Details: System.InvalidOperationException: **The type System.String may not be used in this context.**

[InvalidOperationException: The type System.String may not be used in this context.]
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write216_StrucDocText(String n, String ns, StrucDocText o, Boolean isNullable, Boolean needType) +1823
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write403_Item(String n, String ns, POCD_MT000027TR01SocialSecurityFollowNumberSection o, Boolean isNullable, Boolean needType) +1087
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write404_POCD_MT000027TR01Component45(String n, String ns, POCD_MT000027TR01Component45 o, Boolean isNullable, Boolean needType) +942
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write434_Item(String n, String ns, POCD_MT000027TR01ReceptionDataset o, Boolean isNullable, Boolean needType) +1192
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write435_POCD_MT000027TR01Component44(String n, String ns, POCD_MT000027TR01Component44 o, Boolean isNullable, Boolean needType) +942
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write436_Item(String n, String ns, POCD_MT000027TR01StructuredBody o, Boolean isNullable, Boolean needType) +1477
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write437_POCD_MT000027TR01Component1(String n, String ns, POCD_MT000027TR01Component1 o, Boolean isNullable, Boolean needType) +942
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write438_POCD_MT000027TR01Dialysis(String n, String ns, POCD_MT000027TR01Dialysis o, Boolean isNullable, Boolean needType) +1951
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write439_MCCI_IN000001TR01Subject(String n, String ns, MCCI_IN000001TR01Subject o, Boolean isNullable, Boolean needType) +781
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write440_Item(String n, String ns, MCCI_IN000001TR01ControlActEvent o, Boolean isNullable, Boolean needType) +878
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write441_MCCI_IN000001TR01Message(String n, String ns, MCCI_IN000001TR01Message o, Boolean isNullable, Boolean needType) +1627
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterMCCI_IN000001TR01Message.Write442_MCCI_IN000001TR01(Object o) +144

[InvalidOperationException: There was an error generating the XML document.]
   waSaglikNetUygulamasi._Default.Page_Load(Object sender, EventArgs e) in C:\Users\duygu.akmaz\Desktop\FMC-Projeler\SaglikNet\trunk\Default.aspx.cs:255
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

1 个答案:

答案 0 :(得分:1)

public string ToXml(DataSet ds)
{
  using (var memoryStream = new MemoryStream())
  {
    using(TextWriter streamWriter = new StreamWriter(memoryStream))
    {
      var xmlSerializer = new XmlSerializer(typeof(DataSet));
      xmlSerializer.Serialize(streamWriter, ds);
      return Encoding.UTF8.GetString(memoryStream.ToArray());
    }
  }
}