2017-07-18 30 views
1

私は作成したクラスにいくつかのXMLをデシリアライズしようとすると少し問題があります。VB.Net XMLのクラスへのデシリアライズ

私が手にエラーがある:ここでは

There is an error in XML document (1, 2). 

    at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) 
    at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) 
    at CommonLayer.InvuManager.FindDocuments(String policy, String year) in C:\GIT\novus\CommonLayer\InvuManager.vb:line 194 
    at Novus.NavigationControlRisk.UpdateInvuDocumentsFolderTitle(TreeListNode& documentsFolderNode, String policy, String year) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 3125 
    at Novus.NavigationControlRisk.PopulateFolders(TreeListNode parentNode, Boolean isAttachingPolicy, Boolean refreshData) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 1280 
    at Novus.NavigationControlRisk.PopulateNode(Boolean refreshData) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 1158 
    at Novus.NavigationControlRisk.mainTreeList_MouseClick(Object sender, MouseEventArgs e, Boolean refreshData) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 2340 
    at Novus.NavigationControlRisk._Lambda$__R25-1(Object a0, MouseEventArgs a1) 
    at System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at DevExpress.XtraEditors.Container.EditorContainer.WndProc(Message& m) 
    at DevExpress.XtraTreeList.TreeList.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
    at Novus.My.MyApplication.Main(String[] Args) in :line 81 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

は、私は単にそれが動作するように取得しようとしている、私が作成したクラス、この時点での派手なその何です:

Imports System.Xml.Serialization 

<Serializable, XmlRoot("Document")> _ 
Public Class Document 
    <XmlElement("Type")> _ 
    Public Property Type As String 
    <XmlElement("FileName")> _ 
    Public Property FileName As String 
End Class 

そして、ここで私が使用していたファイルからXMLれる:

<ArrayOfDocuments> 
    <Document> 
    <Type>Debit/Credit note</Type> 
    <FileName>dbE12901_acc1.doc</FileName> 
    </Document> 
    <Document> 
    <Type>Generic</Type> 
    <FileName>a3_lmbc_categories.xls</FileName> 
    </Document> 
</ArrayOfDocuments> 

は最後に、ここで私が使用していたコードは次のとおりです。

Dim foundDocuments As New List(Of Document) 
Dim xmldoc As New XmlDocument 
xmldoc.Load(InterfaceFilePath) 
Dim allText As String = xmldoc.InnerXml 

Using currentStringReader As New StringReader(allText) 
    Dim xml as New XmlSerializer(GetType(List(Of Document))) 
    foundDocuments = TryCast(xml.Deserialize(currentStringReader), List(Of Document)) 
End Using 

私の人生のために、なぜそれがデシリアライズしないのか理解できません。私は私のアプリで別のクラスの別のインスタンスがあり、私はチェックしていると彼らは構造が同じであるので、私はそれが動作しない理由を理解していない。

私は何をしたのかを確認するためにもう一組の目が必要ですが、誰にも示唆がありますか?コメントでクリスで述べたように

+2

を働いた後

はArrayOfDocuments => ArrayOfDocument

を変更する簡単なケースでした。しかし、他の方法を試してみてください。 'Document'のリストを作成し、それを直列化して、生成されたxmlを比較します。それは何が間違っているかをあなたに知らせるかもしれません。 - 私はそれについて考えるので、あなたのXMLのルートは ''(単数形)であってはいけませんか? –

+1

息子の.....仲間のスポット、それはちょうどそれだと信じられない。時にはあなたの間違いを見るために余分な目をかけるだけです! – user3489088

答えて

1

シャウト。それは私はどちらかすぐに間違っているものを見ることができない完全

0

、XMLはルート要素としてArrayOfDocuments(複数)が含まれている場合、それは自動的に動作しませんので、ArrayOfDocument(単数形)であることをList(Of Document)ニーズに使用するルート要素名。

あなたがあるとして、それをデシリアライズするために必要な場合は、1つの簡単な解決策はArrayOfDocumentsクラスを作成し、代わりにList(Of Document)にデシリアライズのそれにデシリアライズするために、次のようになります。

Public Class Document 
    Public Property Type As String 
    Public Property FileName As String 
End Class 

Public Class ArrayOfDocuments 
    <XmlElement> 
    Public Property Document As Document() 
End Class 

そして:

Dim xml As New XmlSerializer(GetType(ArrayOfDocuments)) 
foundDocuments = TryCast(xml.Deserialize(currentStringReader), ArrayOfDocuments) 
3

xmlのテキストをコピーしてVisual Studioで自動的に生成することができます:

クラス

として

編集>>て貼り付け>>貼り付けXML

私はこれをしなかったし、それが(手動 Documentに変更自動名前 ArrayOfDocumentDocument)クラス

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _ 
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _ 
Partial Public Class ArrayOfDocuments 
    Private documentField() As ArrayOfDocumentsDocument 
    <System.Xml.Serialization.XmlElementAttribute("Document")> _ 
    Public Property Document() As ArrayOfDocumentsDocument() 
     Get 
      Return Me.documentField 
     End Get 
     Set(value As ArrayOfDocumentsDocument()) 
      Me.documentField = value 
     End Set 
    End Property 
End Class 

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class ArrayOfDocumentsDocument 
    Private typeField As String 
    Private fileNameField As String 
    Public Property Type() As String 
     Get 
      Return Me.typeField 
     End Get 
     Set(value As String) 
      Me.typeField = value 
     End Set 
    End Property 
    Public Property FileName() As String 
     Get 
      Return Me.fileNameField 
     End Get 
     Set(value As String) 
      Me.fileNameField = value 
     End Set 
    End Property 
End Class 

をもたらしました

これは簡単にデシリアライズされます

上記のコメントの一つに私のためにそれを解決したクリス・ダナウェイのうち
Dim s As New XmlSerializer(GetType(ArrayOfDocuments)) 
Dim m As ArrayOfDocuments 
Using sr As New StreamReader("XMLFile1.xml") 
    m = s.Deserialize(sr) 
End Using 
Dim foundDocuments = m.Document.ToList() 

enter image description here

+1

うわー、それは素晴らしいヒントです、私はそれがVisual Studioにあったことを知らなかった、ありがとう – user3489088

関連する問題