2011-07-18 116 views
0

我收到了以下XML字符串(我已經刪除了實際的命名空間的名稱)問題反序列化XML字符串

<?xml version="1.0" encoding="UTF-8"?> 
- <changeNotificationDto xmlns="http://www.Company.com/item" changeEventTypeCode="RemoveDeliveryPoint" messageId="10" sentDateTime="1970-01-01T00:00:00.000Z"> 
- <entry action="Remove" type="DeliveryPoint"> 
- <removeEntityNotification> 
    <type>DeliveryPoint</type> 
    <compassKey>1139</compassKey> 
    </removeEntityNotification> 
- <changeDelta> 
    <changeDeltaType>ObjectRemove</changeDeltaType> 
    <recordId>1139</recordId> 
    <recordType>DeliveryPoint</recordType> 
    <subRecordId>2324</subRecordId> 
    <subRecordType>ExternalReference</subRecordType> 
    </changeDelta> 
- <changeDelta> 
    <changeDeltaType>ObjectRemove</changeDeltaType> 
    <recordId>1139</recordId> 
    <recordType>DeliveryPoint</recordType> 
    </changeDelta> 
    </entry> 
    </changeNotificationDto> 

我有一個Web參考「changeNotificationDto」,這已經產生了代理類(我沒有任何問題在我的應用程序中創建/使用這種類型的對象)[類型的實際內容發生了變化,但我有來自web引用的代理類,用於我將要接收的所有對象。 出於測試的目的,我一直在使用streamReader從文件中提取xml,但該文件是從原始消息寫入的,因爲通過套接字接收]

我有一個小測試工具,如下所示: - (VB .NEt/VS2010/FrmWrk 4)

 Dim objStreamReader As New StreamReader("C:\Testing\101 VB.NET Samples\SerializeandDeserializeXML\rdp.xml") 
     Dim p2 As ChangeEventManagerService.ChangeNotificationDto 
     Dim output As StringBuilder = New StringBuilder() 

     Using xmlrdr As XmlReader = XmlReader.Create(New StringReader(objStreamReader.ReadToEnd())) 
      Dim ws As XmlWriterSettings = New XmlWriterSettings() 
      ws.Indent = True 
      Using writer As XmlWriter = XmlWriter.Create(output, ws) 

       ' Parse the file and display each of the nodes. 
       While xmlrdr.Read() 
        Select Case xmlrdr.NodeType 
         Case XmlNodeType.Element 
          writer.WriteStartElement(xmlrdr.Name) 
         Case XmlNodeType.Text 
          writer.WriteString(xmlrdr.Value) 
         Case XmlNodeType.XmlDeclaration 
         Case XmlNodeType.ProcessingInstruction 
          writer.WriteProcessingInstruction(xmlrdr.Name, xmlrdr.Value) 
         Case XmlNodeType.Comment 
          writer.WriteComment(xmlrdr.Value) 
         Case XmlNodeType.EndElement 
          writer.WriteFullEndElement() 
        End Select 
       End While 
      End Using 

      Console.Write(output.ToString()) '# up to this point all is good 

      If x.CanDeserialize(xmlrdr) Then '# this fails 
       p2 = CType(x.Deserialize(objStreamReader), ChangeEventManagerService.ChangeNotificationDto) '# forcing it to here throws an error as expected! 
       objStreamReader.Close() 
      End If 

     End Using '# reader 


     ''Display property values of the new product object. 
     Console.WriteLine(p2.changeEventTypeCode) 
     Console.WriteLine(p2.messageId) 


    Catch ex As Exception 
     Console.WriteLine(ex.InnerException.ToString()) 
     Console.WriteLine(ex.ToString()) 
    End Try 

    Console.ReadLine() 

錯誤拋出「根元素丟失」。

任何人都可以幫助我,我有點不知所措。

我推測根元素是元素或者是 元素。

無論如何,我希望我做了所有清楚......

TIA

回答

0

它有額外的字符 ' - ' 在文件的開頭

<?xml version="1.0" encoding="UTF-8"?> 
<changeNotificationDto xmlns="http://www.Company.com/item" changeEventTypeCode="RemoveDeliveryPoint" messageId="10" sentDateTime="1970-01-01T00:00:00.000Z"> 
<entry action="Remove" type="DeliveryPoint"> 
<removeEntityNotification> 
<type>DeliveryPoint</type> 
<compassKey>1139</compassKey> 
</removeEntityNotification> 
<changeDelta> 
<changeDeltaType>ObjectRemove</changeDeltaType> 
<recordId>1139</recordId> 
<recordType>DeliveryPoint</recordType> 
<subRecordId>2324</subRecordId> 
<subRecordType>ExternalReference</subRecordType> 
</changeDelta> 
<changeDelta> 
<changeDeltaType>ObjectRemove</changeDeltaType> 
<recordId>1139</recordId> 
<recordType>DeliveryPoint</recordType> 
</changeDelta> 
</entry> 
</changeNotificationDto> 

上面的xml。當你在IE中打開xml並將其粘貼到記事本中時,就會發生這種情況。它帶有額外的' - '字符。

+0

嗨, 感謝您的回覆.... xml文件沒有隱藏字符或「額外」字符,我可以看到/發現/顯示。 當我創建此主題時,添加了上述xml中的「 - 」。實際的文本字符串中沒有換行符或回車符。 標籤之間有空白,我將刪除。 我還有其他一些性格嗎? – justabloke

+0

我可以知道什麼是x? – ysrb