Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

How to write values correctly in a file.

When I look in my export File I see that my saved values are 7 and not 7.000 the way I typed them into my textbox. What do i need to do so that my values look like 7.000 I thought that declaring the variable as double would allow this to happen.
Imports System.Xml

Public Class Data

  
    'File Data Information
    Public FeatureName As String
    Public FeatureType As String
    Public FeatureComment As String
    Public D As Double
    Public X As Double
    Public Y As Double
    Public Z As Double
    Public USL As Double
    Public LSL As Double
    Public InactivePoint As Boolean
  
    Public Sub WriteXML(ByVal writer As XmlTextWriter)
        'write the top-level tag
        writer.WriteStartElement("Gage")
        'write the details
        writer.WriteElementString("Feature Name", FeatureName)
        writer.WriteElementString("Feature Type", FeatureType)
        writer.WriteElementString("Feature Comment", FeatureComment)
        writer.WriteElementString("D", D)
        writer.WriteElementString("X", X)
        writer.WriteElementString("Y", Y)
        writer.WriteElementString("Z", Z)
        writer.WriteElementString("USL", USL)
        writer.WriteElementString("LSL", LSL)
        writer.WriteElementString("Included Point", InactivePoint)
        'close top-level tag
        writer.WriteEndElement()

    End Sub

    Public Sub ReadXML(ByVal reader As XmlTextReader)
        'loop
        Do While True
            'read
            If reader.Read = False Then
                Exit Do
            End If

            'what node type do we have
            Select Case reader.NodeType
                Case XmlNodeType.Element

                    Select Case reader.Name
                        Case "FeatureName"
                            reader.Read()
                            Me.FeatureName = reader.Value
                        Case "FeatureType"
                            reader.Read()
                            Me.FeatureType = reader.Value
                        Case "FeatureComment"
                            reader.Read()
                            Me.FeatureComment = reader.Value
                        Case "D"
                            reader.Read()
                            Me.D = reader.Value
                        Case "X"
                            reader.Read()
                            Me.X = reader.Value
                        Case "Y"
                            reader.Read()
                            Me.Y = reader.Value
                        Case "Z"
                            reader.Read()
                            Me.Z = reader.Value
                        Case "USL"
                            reader.Read()
                            Me.USL = reader.Value
                        Case "LSL"
                            reader.Read()
                            Me.LSL = reader.Value
                        Case "Inactive Point"
                            reader.Read()
                            Me.InactivePoint = reader.Value
                    End Select

                Case XmlNodeType.EndElement
                    'have we reached the end of the element
                    If reader.Name = "Data" Then
                        Return
                    End If
            End Select
        Loop
    End Sub
End Class

Open in new window

Example-Export.txt
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Use something like this instead:

    writer.WriteElementString("D", D.ToString("0.000"))
Avatar of cmdolcet

ASKER

Idle_Mind that does not work however let me give you more information:
This is how I set the string to write to my file

 Dim gageArray(intInactivePointCol), errstring As String


               FpSpread1.ActiveSheet.SetActiveCell(intLoop, intDCol)
                gageArray(intDCol) = FpSpread1.ActiveSheet.ActiveCell.Value
Can you explain a little more about your last comment?  I think I'd need to see more code...

I simply pasted your sample code and added the ToString() on the line that output "D":

        writer.WriteElementString("D", D.ToString("0.000"))

Then I used this test code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using sfd As New SaveFileDialog
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim MyData As New Data
                MyData.D = 7
                Using Xml As New XmlTextWriter(sfd.FileName, System.Text.Encoding.Default)
                    MyData.WriteXML(Xml)
                End Using
                Process.Start("notepad", sfd.FileName)
            End If
        End Using
    End Sub

Here is the how it looked to me:
FormattedDecimal.jpg
Idle_Mind:

I have attached a scree shot of that variable. It seem that the datatype double does not show the 7.000 value  
Screen-Shot.png
It's probably because you haven't given a type to gageArray and it's being assigned as STRING because of the comma:

    Dim gageArray(intInactivePointCol), errstring As String

If you make it Decimal or Double then you'll probably get different results:

    Dim gageArray(intInactivePointCol) As Double, errstring As String
OK now its telling me value of type 1-dimensional array cannot be converted to 1-dimensional array type string, because double is not derived from string
Can you show me more code where you are using "gageArray"?
Private Function CheckGrid(ByVal gageArray() As String, ByVal intRow As Integer) As String
        Try
            _errorsExist = True
            If Not IsNumeric(CSng(gageArray(intDCol))) Then
                CheckGrid = "Must a Numerical Value " & intRow & "." & vbCrLf & vbCrLf
            ElseIf Not IsNumeric(CSng(gageArray(intXCol))) Then
                CheckGrid = "Must a Numerical Value " & intRow & "." & vbCrLf & vbCrLf
            ElseIf Not IsNumeric(CSng(gageArray(intYCol))) Then
                 CheckGrid = "Must a Numerical Value " & intRow & "." & vbCrLf & vbCrLf
            ElseIf Not IsNumeric(CSng(gageArray(intZCol))) Then
                CheckGrid = "Must a Numerical Value " & intRow & "." & vbCrLf & vbCrLf
            ElseIf Not IsNumeric(CSng(gageArray(intUSLCol))) Then
                CheckGrid = "Must a Numerical Value " & intRow & "." & vbCrLf & vbCrLf
            ElseIf Not IsNumeric(CSng(gageArray(intLSLCol))) Then
                CheckGrid = "Must a Numerical Value " & intRow & "." & vbCrLf & vbCrLf
            Else
                CheckGrid = "Passed"
                _errorsExist = False
            End If
        Catch
            CheckGrid = "General Grid Error!!!"
        End Try
    End Function



Public Sub AddOrDeleteRowsInPartFile()
        Dim intMaxRows As Integer
        Dim intRow As Integer
        Dim gageArray(intInactivePointCol) As String


        intMaxRows = FpSpread1.ActiveSheet.RowCount
        FpSpread1.ActiveSheet.RowCount = PointList.Count

        'Populates the Grid with the Information that is parsed out and saved in the arraylist
        For intRow = intMaxRows To FpSpread1.ActiveSheet.RowCount - 1
            'Fills the Feature Description (Point Name Column)
            FpSpread1.ActiveSheet.SetText(intRow, 0, PointList(intRow).Feature_Description)
            gageArray(intFeatureNameCol) = PointList(intRow).Feature_Description
            'Fills the Feature Type (Point Name Column)
            FpSpread1.ActiveSheet.SetText(intRow, 1, PointList(intRow).Feature_Type)
            gageArray(intFeatureNameCol) = PointList(intRow).Feature_Type
            'Fills the Feature DComment (Long Description Column)
            FpSpread1.ActiveSheet.SetText(intRow, 2, PointList(intRow).Feature_Comment)
            gageArray(intFeatureNameCol) = PointList(intRow).Feature_Comment
            FpSpread1.ActiveSheet.SetText(intRow, 3, PointList(intRow).D_value)
            FpSpread1.ActiveSheet.SetText(intRow, 4, PointList(intRow).Nominal_Value)
            FpSpread1.ActiveSheet.SetText(intRow, 5, PointList(intRow).X_Value)
            FpSpread1.ActiveSheet.SetText(intRow, 6, PointList(intRow).Y_Value)
            FpSpread1.ActiveSheet.SetText(intRow, 7, PointList(intRow).Z_Value)
            FpSpread1.ActiveSheet.SetText(intRow, 8, PointList(intRow).UCL_value)
            FpSpread1.ActiveSheet.SetText(intRow, 9, PointList(intRow).LCL_value)
            FpSpread1.ActiveSheet.SetValue(intRow, 10, PointList(intRow).Include_Feature)
            gageArray(intFeatureNameCol) = PointList(intRow).Include_Feature
            FpSpread1.Update()

            If intRow > _partfile.PartfileData.Count Then
                _partfile.PartfileData.Add(gageArray(intFeatureNameCol), gageArray(intFeatureTypeCol), gageArray(intFeatureCommentCol), CSng(gageArray(intDCol)), CSng(gageArray(intNominalCol)), CSng(gageArray(intXCol)), CSng(gageArray(intYCol)), CSng(gageArray(intZCol)), CSng(gageArray(intUSLCol)), CSng(gageArray(intLSLCol)), True)
            End If
           
        Next
    End Sub
I'd have to see a lot more code to make sense of it all...

...but it seems like you might be unnecessarily passing around an array of strings with "gageArray".   =\

Also, in this code:

            If Not IsNumeric(CSng(gageArray(intDCol))) Then

You convert a string to a single with CSng(), and then pass that to IsNumeric().  If the value was NOT numeric then it would have failed CSng() already...

It could just be:

            If Not IsNumeric(gageArray(intDCol)) Then

In VB.Net, I would have used Single.TryParse() though:

    Dim sng As Single
    If Not Single.TryParse(gageArray(intDCol), sng) Then

Was this code converted from VB6 by chance?
could of been....We have been using this code a lot.

It just seems like there are a ton of "global"/class level variables being used in the code.
(I could be wrong...just judging by the snippets you posted!)

I still don't see how the original modification isn't working.  =\

If your class actually has them declared as Double:

    Public D As Double
    Public X As Double
    Public Y As Double
    Public Z As Double

AND you have modified it so that they ALL have ToString("0,00"):

        writer.WriteElementString("D", D.ToString("0.00"))
        writer.WriteElementString("X", X.ToString("0.00"))
        writer.WriteElementString("Y", Y.ToString("0.00"))
        writer.WriteElementString("Z", Z.ToString("0.00"))

Then it should work...
I have attached more code
Imports System.Xml
Public Class RequestedDataCollection
    Inherits CollectionBase

    'Add - adds a gage
    Public Sub Add(ByVal gage As Data)
        List.Add(gage)
    End Sub

    Public Function Add(ByVal FeatureName As String, ByVal FeatureType As String, ByVal FeatureComment As String, ByVal D As Single, ByVal Nominal As Single, ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal USL As Single, ByVal LSL As Single, ByVal InactivePoint As Boolean)

        'create a new gage
        Dim gage As New Data()
        gage.FeatureName = FeatureName
        gage.FeatureType = FeatureType
        gage.FeatureComment = FeatureComment
        gage.D = D
        gage.X = X
        gage.Nominal = nominal
        gage.Y = Y
        gage.Z = Z
        gage.USL = USL
        gage.LSL = LSL
        gage.InactivePoint = InactivePoint
        'add it
        Add(gage)

        'return it
        Return gage

    End Function

    'Remove - removes the gages
    Public Sub Remove(ByVal gage As Data)
        List.Remove(gage)
    End Sub

    Default Public Property Item(ByVal index As Integer) As Data
        Get
            Return List.Item(index)
        End Get
        Set(ByVal Value As Data)
            List.Item(index) = Value
        End Set
    End Property

    Public Sub WriteXML(ByVal writer As XmlTextWriter)
        'write the top-level tag
        writer.WriteStartElement("Gages")

        'go through each gage
        Dim gage As Data
        For Each gage In Innerlist
            gage.WriteXML(writer)
        Next

        'close top level tag
        writer.WriteEndElement()

    End Sub

    Public Sub ReadXML(ByVal reader As XmlTextReader)
        'loop
        Do While True
            'read
            If reader.Read = False Then
                Exit Do
            End If

            'what node type do we have
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    'do we have a gage element
                    If reader.Name = "Data" Then
                        'create one
                        Dim gage As New Data()
                        gage.ReadXML(reader)
                        'add it
                        Add(gage)
                    End If

                Case XmlNodeType.EndElement
                    'have we reached the end of the element
                    If reader.Name = "PartfileData" Then
                        Return
                    End If
            End Select
        Loop
    End Sub
End Class

Open in new window

That could shouldn't have any affect on the output...it's the DATA class that is actually writing no?

Again, make sure you've changed them all to output using the desired format:

        writer.WriteElementString("D", D.ToString("0.00"))
        writer.WriteElementString("X", X.ToString("0.00"))
        writer.WriteElementString("Y", Y.ToString("0.00"))
        writer.WriteElementString("Z", Z.ToString("0.00"))
Yes it is.
Make a quick test app to see what is output...just a Form a Button with the following code:
Imports System.Xml
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using sfd As New SaveFileDialog
            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim MyData As New Data
                MyData.D = 7
                MyData.X = 8
                MyData.Y = 9
                MyData.Z = 10
                Using Xml As New XmlTextWriter(sfd.FileName, System.Text.Encoding.Default)
                    MyData.WriteXML(Xml)
                End Using
                Process.Start("notepad", sfd.FileName)
            End If
        End Using
    End Sub

End Class

Public Class Data

    'File Data Information
    Public FeatureName As String
    Public FeatureType As String
    Public FeatureComment As String
    Public D As Double
    Public X As Double
    Public Y As Double
    Public Z As Double
    Public USL As Double
    Public LSL As Double
    Public InactivePoint As Boolean
  
    Public Sub WriteXML(ByVal writer As XmlTextWriter)
        'write the top-level tag
        writer.WriteStartElement("Gage")
        'write the details
        writer.WriteElementString("Feature Name", FeatureName)
        writer.WriteElementString("Feature Type", FeatureType)
        writer.WriteElementString("Feature Comment", FeatureComment)
        writer.WriteElementString("D", D.ToString("0.00"))
        writer.WriteElementString("X", X.ToString("0.00"))
        writer.WriteElementString("Y", Y.ToString("0.00"))
        writer.WriteElementString("Z", Z.ToString("0.00"))
        writer.WriteElementString("USL", USL)
        writer.WriteElementString("LSL", LSL)
        writer.WriteElementString("Included Point", InactivePoint)
        'close top-level tag
        writer.WriteEndElement()
    End Sub

    Public Sub ReadXML(ByVal reader As XmlTextReader)
        'loop
        Do While True
            'read
            If reader.Read = False Then
                Exit Do
            End If

            'what node type do we have
            Select Case reader.NodeType
                Case XmlNodeType.Element

                    Select Case reader.Name
                        Case "FeatureName"
                            reader.Read()
                            Me.FeatureName = reader.Value
                        Case "FeatureType"
                            reader.Read()
                            Me.FeatureType = reader.Value
                        Case "FeatureComment"
                            reader.Read()
                            Me.FeatureComment = reader.Value
                        Case "D"
                            reader.Read()
                            Me.D = reader.Value
                        Case "X"
                            reader.Read()
                            Me.X = reader.Value
                        Case "Y"
                            reader.Read()
                            Me.Y = reader.Value
                        Case "Z"
                            reader.Read()
                            Me.Z = reader.Value
                        Case "USL"
                            reader.Read()
                            Me.USL = reader.Value
                        Case "LSL"
                            reader.Read()
                            Me.LSL = reader.Value
                        Case "Inactive Point"
                            reader.Read()
                            Me.InactivePoint = reader.Value
                    End Select

                Case XmlNodeType.EndElement
                    'have we reached the end of the element
                    If reader.Name = "Data" Then
                        Return
                    End If
            End Select
        Loop
    End Sub

End Class

Open in new window

Yes this works just fine now how can I set mine up like this
Is WriteXML() the only place that outputs to the file?  If so, do a search for it and let me know...

If not, show me the other code that writes to the file.
Ok I have attached the code. I did not see any extra WriteXML()
 'Saves the .XML file
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
        ValidateGridControl()
        If ValidateGridControl() = False Then
        Else
            btnSaveActive = True
            _partfile.Save(dir_name_XML_Files & File_Selected_No_Extensions & ".xml")
            frmMain.ChangeIndexFileColor()
            Close()
        End If
    End Sub

 Private Function ValidateGridControl() As Boolean
        Dim intLoop, intloop2 As Integer
        Dim gageArray(intInactivePointCol), errstring As String
        ' Dim gageArray(intInactivePointCol) As Double, errstring As String
        Try

            If Me.cmbReleaseOrganization.Text.Length > 10 Then
                errstring = "Release Organization Code is too Long." & vbCrLf & "Max Size 10 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Release Organization Code")
                ErrorProvider.SetError(Me.cmbReleaseOrganization, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            If Me.cmbVehicleFamily.Text.Length > 5 Then
                errstring = "Vehicle Family Code is too Long." & vbCrLf & "Max Size 5 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Vehicle Family Code")
                ErrorProvider.SetError(Me.cmbVehicleFamily, errstring)
                ValidateGridControl = False
                Exit Function
            End If


            If Me.cmbDivision.Text.Length > 12 Then
                errstring = "Division Code is too Long." & vbCrLf & "Max Size 12 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Vehicle Family Code")
                ErrorProvider.SetError(Me.cmbDivision, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            If Me.cmbSupplierCode.Text.Length > 6 Then
                errstring = "Supplier Code is too Long." & vbCrLf & "Max Size 6 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Supplier Code")
                ErrorProvider.SetError(Me.cmbSupplierCode, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            If Me.cmbReleaseOrganization.Text.Length > 16 Then
                errstring = "Site Code is too Long." & vbCrLf & "Max Size 16 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Site Code")
                ErrorProvider.SetError(Me.cmbSite, errstring)
                ValidateGridControl = False
                Exit Function
            End If


            If Me.cmbReleaseOrganization.Text.Length > 33 Then
                errstring = "Program Name is too Long." & vbCrLf & "Max Size 33characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Program Name")
                ErrorProvider.SetError(Me.cmbPartFileName, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            If Me.cmbReleaseOrganization.Text.Length > 20 Then
                errstring = "Data Measure Type Code is too Long." & vbCrLf & "Max Size 20 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Data Measure Type Code")
                ErrorProvider.SetError(Me.cmbDataMeasured, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            If Me.cmbReleaseOrganization.Text.Length > 12 Then
                errstring = "Part/Tool Number Code is too Long." & vbCrLf & "Max Size 12 characters" & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Part/Tool Number Code")
                ErrorProvider.SetError(Me.cmbPartToolNumber, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            If Me.cmbReleaseOrganization.Text.Length > 31 Then
                errstring = "Part/Tool Name Code is too Long." & vbCrLf & "Max Size 31 characters. " & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Part/Tool Name Code")
                ErrorProvider.SetError(Me.cmbPartToolName, errstring)
                ValidateGridControl = False
                Exit Function
            End If

            'If Me.cmbReleaseOrganization.Text.Length > 10 Then
            '    errstring = "SEQ Number Code is too Long." & vbCrLf & "Max Size 10 characters" & "Please correct this before saving."
            '    MsgBox(errstring, MsgBoxStyle.OkOnly, "SSEQ Number Code")
            '    ErrorProvider.SetError(Me.cmb, errstring)
            '    Exit Function
            'End If

            'If Me.cmbReleaseOrganization.Text.Length > 10 Then
            '    errstring = "LAB Number Code is too Long." & vbCrLf & "Max Size 10 characters" & "Please correct this before saving."
            '    MsgBox(errstring, MsgBoxStyle.OkOnly, "LAB Number Code")
            '    ErrorProvider.SetError(Me.cmbReleaseOrganization, errstring)
            '    Exit Function
            'End If

            If Me.cmbBuildType.Text.Length > 3 Then
                errstring = "Build Type Code is too Long." & vbCrLf & "Max Size 3 characters. " & "Please correct this before saving."
                MsgBox(errstring, MsgBoxStyle.OkOnly, "Build Type Code")
                ErrorProvider.SetError(Me.cmbBuildType, errstring)
                ValidateGridControl = False
                Exit Function
            End If


            'If Me.cmbReleaseOrganization.Text.Length > 20 Then
            '    errstring = "CMM Data Format Code is too Long." & vbCrLf & "Max Size 20characters" & "Please correct this before saving."
            '    MsgBox(errstring, MsgBoxStyle.OkOnly, "CMM Data Format Code")
            '    ErrorProvider.SetError(Me.cmbReleaseOrganizati, errstring)
            '    Exit Function
            'End If

            'Checks the Grid for Errors.
            For intLoop = 0 To _partfile.PartfileData.Count - 1
                'Validate the gage 
                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intFeatureNameCol)
                gageArray(intFeatureNameCol) = FpSpread1.ActiveSheet.ActiveCell.Text.TrimEnd

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intFeatureTypeCol)
                gageArray(intFeatureTypeCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intFeatureCommentCol)
                gageArray(intFeatureCommentCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intDCol)
                gageArray(intDCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intNominalCol)
                gageArray(intNominalCol) = FpSpread1.ActiveSheet.ActiveCell.Text 
                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intXCol)
                gageArray(intXCol) = FpSpread1.ActiveSheet.ActiveCell.Text


                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intYCol)
                gageArray(intYCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intZCol)
                gageArray(intZCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intUSLCol)
                gageArray(intUSLCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intLSLCol)
                gageArray(intLSLCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                FpSpread1.ActiveSheet.SetActiveCell(intLoop, intInactivePointCol)
                gageArray(intInactivePointCol) = FpSpread1.ActiveSheet.ActiveCell.Text

                
                For intloop2 = 0 To FpSpread1.ActiveSheet.RowCount
                    If Not intloop2 = intLoop Then
                        FpSpread1.ActiveSheet.SetActiveCell(intloop2, intFeatureNameCol)
                        If (gageArray(intFeatureNameCol) = FpSpread1.ActiveSheet.ActiveCell.Text) And (intLoop < FpSpread1.ActiveSheet.RowCount) Then
                            errstring = "Row " & intLoop + 1 & " has the same name as row " & intloop2 + 1 & ". Please correct this."
                            MsgBox(errstring, MsgBoxStyle.OkOnly, "Grid Error...")
                            ErrorProvider.SetError(FpSpread1, errstring)
                            ValidateGridControl = False
                            Exit Function
                        End If
                    End If
                Next


                If Not CheckGrid(gageArray, intLoop + 1) = "Passed" Then
                    errstring = CheckGrid(gageArray, intLoop + 1)
                    MsgBox(errstring)
                    errstring += vbCrLf & vbCrLf & "This error has appeared because there is something wrong with the grid values that were entered!" & vbCrLf
                    errstring += "It will disappear once the grid values have been corrected!"
                    Me.ErrorProvider.SetError(FpSpread1, errstring)
                    ValidateGridControl = False
                    Exit Function
                Else
                    _partfile.FileHeader.ReleaseOrginazation = Me.cmbReleaseOrganization.Text
                    _partfile.FileHeader.Site = Me.cmbSite.Text
                    _partfile.FileHeader.Division = Me.cmbDivision.Text
                    _partfile.FileHeader.VehicleFamily = Me.cmbVehicleFamily.Text
                    _partfile.FileHeader.SupplierCode = Me.cmbSupplierCode.Text
                    _partfile.FileHeader.ProgramName = Me.cmbPartFileName.Text
                    _partfile.FileHeader.DataMeasuredType = Me.cmbDataMeasured.Text
                    _partfile.FileHeader.DateTimeMeasured = Me.cmbDataMeasured.Text
                    _partfile.FileHeader.BuildType = Me.cmbBuildType.Text
                    _partfile.FileHeader.SEQNumber = "" 'gageArray(intInactivePointCol)
                    _partfile.FileHeader.LABNumber = "" 'gageArray(intYCol)
                    _partfile.FileHeader.PartToolName = Me.cmbPartToolName.Text
                    _partfile.FileHeader.PartToolNumber = Me.cmbPartToolNumber.Text
                    _partfile.FileHeader.OperatorID = "" 'gageArray(intLSLCol)
                    _partfile.FileHeader.CMMDataFormat = Export_Extension


                    _partfile.PartfileData(intLoop).FeatureName = gageArray(intFeatureNameCol)
                    _partfile.PartfileData(intLoop).FeatureType = gageArray(intFeatureTypeCol)
                    _partfile.PartfileData(intLoop).FeatureComment = gageArray(intFeatureCommentCol)
                    _partfile.PartfileData(intLoop).Nominal = gageArray(intNominalCol)
                    _partfile.PartfileData(intLoop).D = gageArray(intDCol)
                    _partfile.PartfileData(intLoop).X = gageArray(intXCol)
                    _partfile.PartfileData(intLoop).Y = gageArray(intYCol)
                    _partfile.PartfileData(intLoop).Z = gageArray(intZCol)
                    _partfile.PartfileData(intLoop).USL = gageArray(intUSLCol)
                    _partfile.PartfileData(intLoop).LSL = gageArray(intLSLCol)
                    _partfile.PartfileData(intLoop).InactivePoint = gageArray(intInactivePointCol)
                    Me.ErrorProvider.SetError(FpSpread1, "")
                End If
            Next
            ValidateGridControl = True
        Catch
            ValidateGridControl = False
            MsgBox("Grid Validation Error - " + Err.Description)
        Finally
        End Try
    End Function



Imports System.Xml
Public Class Data
    'File Data Information
    Public FeatureName As String
    Public FeatureType As String
    Public FeatureComment As String
    Public D As Double
    Public Nominal As Double
    Public X As Double
    Public Y As Double
    Public Z As Double
    Public USL As Double
    Public LSL As Double
    Public InactivePoint As Boolean
  
    Public Sub WriteXML(ByVal writer As XmlTextWriter)
        'write the top-level tag
        writer.WriteStartElement("Gage")
        'write the details
        writer.WriteElementString("Feature Name", FeatureName)
        writer.WriteElementString("Feature Type", FeatureType)
        writer.WriteElementString("Feature Comment", FeatureComment)
        writer.WriteElementString("D", D.ToString("0.000")) 'D)
        writer.WriteElementString("D", D.ToString("0.000"))
        writer.WriteElementString("Nominal", Nominal.ToString("0.000")) 'D)
        writer.WriteElementString("X", X.ToString("0.000"))
        writer.WriteElementString("Y", Y.ToString("0.000"))
        writer.WriteElementString("Z", Z.ToString("0.000"))
        writer.WriteElementString("USL", USL.ToString("0.000"))
        writer.WriteElementString("LSL", LSL.ToString("0.000"))
        writer.WriteElementString("Included Point", InactivePoint)

        'close top-level tag
        writer.WriteEndElement()

    End Sub

    Public Sub ReadXML(ByVal reader As XmlTextReader)
        'loop
        Do While True
            'read
            If reader.Read = False Then
                Exit Do
            End If

            'what node type do we have
            Select Case reader.NodeType
                Case XmlNodeType.Element

                    Select Case reader.Name
                        Case "FeatureName"
                            reader.Read()
                            Me.FeatureName = reader.Value
                        Case "FeatureType"
                            reader.Read()
                            Me.FeatureType = reader.Value
                        Case "FeatureComment"
                            reader.Read()
                            Me.FeatureComment = reader.Value
                        Case "D"
                            reader.Read()
                            Me.D = reader.Value

                        Case "Nominal"
                            reader.Read()
                            Me.Nominal = reader.Value

                        Case "X"
                            reader.Read()
                            Me.X = reader.Value
                        Case "Y"
                            reader.Read()
                            Me.Y = reader.Value
                        Case "Z"
                            reader.Read()
                            Me.Z = reader.Value
                        Case "USL"
                            reader.Read()
                            Me.USL = reader.Value
                        Case "LSL"
                            reader.Read()
                            Me.LSL = reader.Value
                        Case "Inactive Point"
                            reader.Read()
                            Me.InactivePoint = reader.Value
                    End Select

                Case XmlNodeType.EndElement
                    'have we reached the end of the element
                    If reader.Name = "Data" Then
                        Return
                    End If
            End Select
        Loop
    End Sub
End Class







Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Partfile
    'members
    Public PartfileData As New RequestedDataCollection()
    Public FileHeader As New FileHeader

    'Save - saves the part file to a file
    Public Sub Save(ByVal filename As String)
        'do we need to delete the file
        Dim info As New FileInfo(filename)
        If info.Exists Then
            info.Delete()
        End If

        'create the new file
        Dim stream As New FileStream(filename, FileMode.Create)

        'save it
        Dim serializer As New XmlSerializer(Me.GetType)
        serializer.Serialize(stream, Me)

        'close the file
        stream.Close()
    End Sub

    'ReadXML - load the part file
    Public Sub ReadXML(ByVal reader As XmlTextReader)
        'read through the elements
        Do While True
            If reader.Read = False Then
                Exit Do
            End If

            'What node type do we have
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    Select Case reader.Name
                        Case "PartfileData"
                            Me.PartfileData.ReadXML(reader)
                        Case "FileHeader"
                            Me.FileHeader.ReadXML(reader.Name, reader)
                    End Select
            End Select
        Loop

    End Sub

Open in new window

Ok...I see this at line #7:

    _partfile.Save(dir_name_XML_Files & File_Selected_No_Extensions & ".xml")

In PartFile.Save() you're are simply telling the class to serialize itself to XML:

        'save it
        Dim serializer As New XmlSerializer(Me.GetType)
        serializer.Serialize(stream, Me)

Which in turn causes the collection of Data instances to be serialized as well:

    Public PartfileData As New RequestedDataCollection()

BUT this is just going to use the DEFAULT XML serializer to write out the public members of the Data Class.

This isn't going to make Data.WriteXML() be called....thus you won't get the formatting we specified with ToString().

Unless you've OMITTED that Data actually Implements IXMLTransferable?

Then it should look like:

    Public Class Data
        Implements IXMLTransferable

    End Class

And you tag the WriteXML/ReadXML methods with "Implements xxx.yyy":
http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

For example:

        Public Sub WriteXML(ByVal writer As XmlTextWriter) Implements IXMLTransferable.WriteXML

See:
http://www.devx.com/dotnet/Article/29720/1954
Idle_Mind: Ok so do i remove that line and put the
  Public Sub WriteXML(ByVal writer As XmlTextWriter) Implements IXMLTransferable.WriteXML

instead of:

 Public Sub WriteXML(ByVal writer As XmlTextWriter)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Idle_Mind: Ok so why isnt my code working then? I dont see a difference
So you've got your Data class successfully implementing System.Xml.Serialization.IXmlSerializable?

    Public Class Data
        Implements System.Xml.Serialization.IXmlSerializable
No why can;t i just use my existing code?
Well...in your Data class, the CUSTOM formatting was placed into the WriteXML() method BUT that method does NOT get called with the DEFAULT XML serialization method (which is being called when the PartFile class with the RequestedDataCollection class within is serialized).  To get your custom WriteXML() method to be called the class has to Implement System.Xml.Serialization.IXmlSerializable...that's how it works!
Idle_Mind:
Ok im getting errors on my Fileinfo and FileStream and XmlSerializer are all saying they are not defied.
Make sure you've got the right Imports:

    Imports System
    Imports System.IO
    Imports System.Xml
    Imports System.Xml.Schema
    Imports System.Xml.Serialization
Idle_Mind:  OK I want to try your earlier suggestion in the is post. However my fileCheckDates strip off everything and returns the just the file name.

This line
  ' _partfile.Save(_dir_name_XML_Files & FileCheckDates & ".xml")

Added in the default sirectory that I set earlier in the program the file name and the extension.

My question is using your code below... How can I create that same .xml file?


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
        Dim FileCheckDates As String

        If ValidateGridControl() = False Then
        Else
            btnSaveActive = True
            FileCheckDates = File_Selected_No_Extensions
            FileCheckDates = System.IO.Path.GetFileNameWithoutExtension(FileCheckDates)
            If FileCheckDates Like "*########" Then
                FileCheckDates = FileCheckDates.Remove(FileCheckDates.Length - 12)
            End If


            Using sfd As New SaveFileDialog
                sfd.FileName = FileCheckDates
                Dim DataValue As New Data
                DataValue.D = 7
                DataValue.X = 8
                DataValue.Y = 9
                DataValue.Z = 10
                Using Xml As New XmlTextWriter(sfd.FileName, System.Text.Encoding.Default)
                    DataValue.WriteXML(Xml)
                End Using

            End Using
                    frmMain.ChangeIndexFileColor()
            Close()
        End If
    End Sub

Open in new window

Not sure I follow...do you want the SaveFileDialog or not?

If not, just plug it in:

               Dim XML_File As String = _dir_name_XML_Files & FileCheckDates & ".xml"
               Using Xml As New XmlTextWriter(XML_File, System.Text.Encoding.Default)
                    DataValue.WriteXML(Xml)
                End Using
Ok then how would I get around this code then?
 _partfile.FileHeader.ReleaseOrganization = Me.cmbReleaseOrganization.Text
                _partfile.FileHeader.Site = Me.cmbSite.Text
                _partfile.FileHeader.Division = Me.cmbDivision.Text
                _partfile.FileHeader.VehicleFamily = Me.cmbVehicleFamily.Text
                _partfile.FileHeader.SupplierCode = Me.cmbSupplierCode.Text
                _partfile.FileHeader.ProgramName = Me.cmbPartFileName.Text
                _partfile.FileHeader.DataMeasuredType = Me.cmbDataMeasured.Text
                _partfile.FileHeader.DateTimeMeasured = Me.cmbDateTimeMeasured.Text
                _partfile.FileHeader.BuildType = Me.cmbBuildType.Text
                _partfile.FileHeader.SEQNumber = Me.cmbSEQNumber.Text
                _partfile.FileHeader.LABNumber = Me.cmbLABNumber.Text
                _partfile.FileHeader.PartToolName = Me.cmbPartToolName.Text
                _partfile.FileHeader.PartToolNumber = Me.cmbPartToolNumber.Text
                _partfile.FileHeader.OperatorID = Me.cmbOperatorID.Text
                _partfile.FileHeader.CMMDataFormat = Export_Extension
                _partfile.FileHeader.GridCount = CountRows


                _partfile.PartfileData(intLoop).FeatureName = gageArray(intFeatureNameCol)
                _partfile.PartfileData(intLoop).FeatureType = gageArray(intFeatureTypeCol)

                If gageArray(intFeatureCommentCol).Length > 50 Then
                    FeatureCommentStr = gageArray(intFeatureCommentCol)
                    FeatureCommentStr = FeatureCommentStr.Substring(0, 50)
                    gageArray(intFeatureCommentCol) = FeatureCommentStr
                    _partfile.PartfileData(intLoop).FeatureComment = gageArray(intFeatureCommentCol)
                Else
                    _partfile.PartfileData(intLoop).FeatureComment = gageArray(intFeatureCommentCol)
                End If

                _partfile.PartfileData(intLoop).Nominal = gageArray(intNominalCol)
                _partfile.PartfileData(intLoop).D = gageArray(intDCol)
                _partfile.PartfileData(intLoop).X = gageArray(intXCol)
                _partfile.PartfileData(intLoop).Y = gageArray(intYCol)
                _partfile.PartfileData(intLoop).Z = gageArray(intZCol)
                _partfile.PartfileData(intLoop).USL = gageArray(intUSLCol)
                _partfile.PartfileData(intLoop).LSL = gageArray(intLSLCol)
                _partfile.PartfileData(intLoop).InactivePoint = gageArray(intInactivePointCol)

Open in new window

There are two different issues here:
(1) Serializing the PartFile Collection
(2) Serializing the Data Class

If you Implement the Interface in the Data Class as demonstrated back in this post:
https://www.experts-exchange.com/questions/26553949/How-to-write-values-correctly-in-a-file.html#33969228

...then you should be able to do (1) as you already are and it will use the custom formatting you specificied for Data.