none
vb 2010 express xml mal formé RRS feed

  • Question

  • Bonjour,

    j'essaye d'écrire un petit programme pour lire et écrire des fichiers xml simples mais à chaque fois que je tente de lire un fichier que j'ai sauvegardé, il m'indique une erreur.

    J'apprécierai vos retours quant à la source de l'erreur qui empêche mon programme de fonctionner!

    Lorsque j'ouvre le fichier XML en question, Firefox m'indique Erreur d'analyse XML: mal formé

    Emplacement : file:///D:/ etc..

    Numéro de ligne 2, Colonne 15 :

    <Repair Orders>
           --------------^

    Le code créant le fichier XML et le sauvegardant est le suivant:

    Private Sub MnuFileSave_Click(sender As Object, e As System.EventArgs) Handles MnuFileSave.Click
            'Just in case, calculate the order now
            Calculate()
    
            'This number will be used to incrementally create the files by their names
            Dim iFilename As Integer
            Dim FileName As String
    
            'If this Directory doesn't exist, you have to create it
            ' Also, get a reference to this directory for later use
    
            Dim FolderName As String = "D:\Australie\Web Programming\Exemples VBA\College Park Auto Repairs"
            Dim Folder As DirectoryInfo = Directory.CreateDirectory(FolderName)
    
            'Get the list of files, if any, from the above folder
            Dim ListOfFiles() As FileInfo = Folder.GetFiles("*.xml")
    
            'If There is no XML File in the directory
            'Then get ready to create the first file
            If ListOfFiles.Length = 0 Then
                'Create the name of the initial file
                iFilename = 1000
            Else
                'If there was at least one file in the directory
                'Get a reference to the last file
                Dim fleLast As FileInfo = ListOfFiles(ListOfFiles.Length - 1)
    
                'Get the name of the last file, without its extension
                Dim fwe As String = Path.GetFileNameWithoutExtension(fleLast.FullName)
    
                'Get the name of the file
                iFilename = CInt(fwe)
    
            End If
    
            FileName = FolderName & "\" & CStr(iFilename + 1) & ".xml"
            'Get ready to display it in the Save dialog box
            dlgSave.FileName = FileName
    
            'Find out if the user clicked OK after displaying the Save Dialog Box
            If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim wtrRepairOrder As XmlTextWriter = New XmlTextWriter(FileName, Encoding.UTF8)
    
                wtrRepairOrder.Formatting = Formatting.Indented
                wtrRepairOrder.Indentation = 4
    
    
                'Create the content of the XML File
                'Notice that we are not making an attempt to check the values
    
                wtrRepairOrder.WriteStartDocument()
    
                wtrRepairOrder.WriteStartElement("Repair Orders")
                wtrRepairOrder.WriteStartElement("Invoice")
    
                wtrRepairOrder.WriteElementString("CustomerName", txtCustomerName.Text)
                wtrRepairOrder.WriteElementString("Address", txtAddress.Text)
                wtrRepairOrder.WriteElementString("City", txtCity.Text)
                wtrRepairOrder.WriteElementString("State", txtState.Text)
                wtrRepairOrder.WriteElementString("ZIPCode", txtZipCode.Text)
                wtrRepairOrder.WriteElementString("Make", txtMake.Text)
                wtrRepairOrder.WriteElementString("Model", txtModel.Text)
                wtrRepairOrder.WriteElementString("CarYear", txtCarYear.Text)
    
                wtrRepairOrder.WriteElementString("Problem Description", txtProblem.Text)
    
                wtrRepairOrder.WriteElementString("PartName1", txtPartName1.Text)
                wtrRepairOrder.WriteElementString("UnitPrice1", txtUnitPrice1.Text)
                wtrRepairOrder.WriteElementString("Quantity1", txtQuantity1.Text)
                wtrRepairOrder.WriteElementString("SubTotal1", txtSubTotal1.Text)
    
                wtrRepairOrder.WriteElementString("PartName2", txtPartName2.Text)
                wtrRepairOrder.WriteElementString("UnitPrice2", txtUnitPrice2.Text)
                wtrRepairOrder.WriteElementString("Quantity2", txtQuantity2.Text)
                wtrRepairOrder.WriteElementString("SubTotal2", txtSubTotal2.Text)
    
                wtrRepairOrder.WriteElementString("PartName3", txtPartName3.Text)
                wtrRepairOrder.WriteElementString("UnitPrice3", txtUnitPrice3.Text)
                wtrRepairOrder.WriteElementString("Quantity3", txtQuantity3.Text)
                wtrRepairOrder.WriteElementString("SubTotal3", txtSubTotal3.Text)
    
                wtrRepairOrder.WriteElementString("PartName4", txtPartName4.Text)
                wtrRepairOrder.WriteElementString("UnitPrice4", txtUnitPrice4.Text)
                wtrRepairOrder.WriteElementString("Quantity4", txtQuantity4.Text)
                wtrRepairOrder.WriteElementString("SubTotal4", txtSubTotal4.Text)
    
                wtrRepairOrder.WriteElementString("PartName5", txtPartName5.Text)
                wtrRepairOrder.WriteElementString("UnitPrice5", txtUnitPrice5.Text)
                wtrRepairOrder.WriteElementString("Quantity5", txtQuantity5.Text)
                wtrRepairOrder.WriteElementString("SubTotal5", txtSubTotal5.Text)
    
                wtrRepairOrder.WriteElementString("JobDescription1", txtJobPerformed1.Text)
                wtrRepairOrder.WriteElementString("JobPrice1", txtJobPrice1.Text)
                wtrRepairOrder.WriteElementString("JobDescription2", txtJobPerformed2.Text)
                wtrRepairOrder.WriteElementString("JobPrice2", txtJobPrice2.Text)
                wtrRepairOrder.WriteElementString("JobDescription3", txtJobPerformed3.Text)
                wtrRepairOrder.WriteElementString("JobPrice3", txtJobPrice3.Text)
                wtrRepairOrder.WriteElementString("JobDescription4", txtJobPerformed4.Text)
                wtrRepairOrder.WriteElementString("JobPrice4", txtJobPrice4.Text)
                wtrRepairOrder.WriteElementString("JobDescription5", txtJobPerformed5.Text)
                wtrRepairOrder.WriteElementString("JobPrice5", txtJobPrice5.Text)
                wtrRepairOrder.WriteElementString("TotalPart", txtTotalParts.Text)
                wtrRepairOrder.WriteElementString("TotalLabor", txtTotalLabour.Text)
                wtrRepairOrder.WriteElementString("TaxRate", txtTaxRate.Text)
                wtrRepairOrder.WriteElementString("TaxAmount", txtTaxAmount.Text)
                wtrRepairOrder.WriteElementString("TotalOrder", txtTotalOrder.Text)
                wtrRepairOrder.WriteElementString("Recommendation", txtRecommendations.Text)
    
                wtrRepairOrder.WriteEndElement()
                wtrRepairOrder.WriteEndElement()
    
                wtrRepairOrder.WriteEndDocument()
    
                wtrRepairOrder.Flush()
                wtrRepairOrder.Close()
    
            End If
    
        End Sub

    Merci de votre aide

    D

    mardi 28 mai 2013 07:27

Réponses

  • En fait j'ai trouvé l'erreur moi-même, les éléments XML ne peuvent contenir d'espace dans leur nom!!

    ainsi <Repair Order> n'est pas possible, <RepairOrder> est la version correcte!

    • Marqué comme réponse Londonfrench mardi 28 mai 2013 09:06
    mardi 28 mai 2013 09:06