2011-07-18 5 views
0

Je reçois la chaîne XML suivante (je l'ai supprimé le nom d'espace de noms réel)Problème chaîne XML désérialisation

<?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> 

J'ai une référence Web à la « changeNotificationDto » et cela a généré une classe proxy (Je n'ai aucun problème à créer/utiliser des objets de ce type dans mon application) [Le contenu réel du type change mais j'ai des classes proxy à partir de références web pour tous les objets que je vais recevoir. Pour les besoins du test, j'ai utilisé streamReader pour extraire le fichier XML d'un fichier mais le fichier a été écrit à partir du message reçu par une socket]

J'ai un petit faisceau de test comme suit: - (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() 

L'erreur jeté "élément racine est manquant".

Quelqu'un peut-il m'aider avec cela, je suis un peu perdu. J'ai supposé que l'élément racine était soit soit l'élément.

Quoi qu'il en soit, je l'espère, je l'ai fait que tout clair ...

TIA

jab

Répondre

0

Il a de plus char '-' au début du fichier

<?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> 

Le xml ci-dessus. Cela se produit lorsque vous ouvrez le fichier XML dans IE et le collez dans le bloc-notes. Il porte les caractères '-' supplémentaires.

+0

Salut, Merci pour votre réponse .... le fichier xml n'a aucuns caractères cachés ou caractères "extra" que je peux voir/trouver/révéler. Les "-" dans le fichier XML ci-dessus ont été ajoutés lorsque j'ai créé ce sujet. La chaîne de texte proprement dite n'a pas de saut de ligne ou de retour chariot. Il y a un espace entre les balises que je vais supprimer. Y a-t-il un autre type de personnage qui me manque? – justabloke

+0

Puis-je savoir ce qu'est x? – ysrb