How do I read information froma file?

I keep on coming across situations where reading data from a file would be incredibly useful. Are there any standard methods to get at the data I need, ignoring the stuff I don't want and placing that data somewhere where I can manipulate it in my program.

Currently I want to read subtitle data from an XML based subtitle file. The data looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<DCSubtitle Version="1.0">
 <SubtitleID>SomeID</SubtitleID>
 <MovieTitle>Some Movie</MovieTitle>
 <ReelNumber>1</ReelNumber>
 <Language>French</Language>
 <Font Italic="no">
  <Font Italic="yes">
   <Subtitle SpotNumber="1" TimeIn="10:00:58:080" TimeOut="10:01:01:220" FadeUpTime="20" FadeDownTime="20">
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="14.0">some Caption</Text>
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="6.0">Some Caption</Text>
   </Subtitle>
  </Font>
  <Font Italic="yes">
   <Subtitle SpotNumber="2" TimeIn="10:01:02:020" TimeOut="10:01:05:220" FadeUpTime="20" FadeDownTime="20">
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="14.0">Some Caption</Text>
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="6.0">Some Caption</Text>
   </Subtitle>
  </Font>
</DCSubtitle>

What can I do with this?
davidogdenblueAsked:
Who is Participating?
 
iboutchkineCommented:
First several comments about your xml
1.Yor xml is not formatted properly
End tag 'DCSubtitle' does not match the start tag 'Font'.
2.The same table (Font) cannot be the child table in two nested relations.

I would changed this to the following xml
<?xml version="1.0" encoding="UTF-8"?>
<DCSubtitle Version="1.0">
 <SubtitleID>SomeID</SubtitleID>
 <MovieTitle>Some Movie</MovieTitle>
 <ReelNumber>1</ReelNumber>
 <Language>French</Language>
 <Font1 Italic="no">
  <Font Italic="yes">
   <Subtitle SpotNumber="1" TimeIn="10:00:58:080" TimeOut="10:01:01:220" FadeUpTime="20" FadeDownTime="20">
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="14.0">some Caption</Text>
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="6.0">Some Caption</Text>
   </Subtitle>
  </Font>
  <Font Italic="yes">
   <Subtitle SpotNumber="2" TimeIn="10:01:02:020" TimeOut="10:01:05:220" FadeUpTime="20" FadeDownTime="20">
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="14.0">Some Caption</Text>
    <Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="6.0">Some Caption</Text>
   </Subtitle>
  </Font>
 </Font1>
</DCSubtitle>

after that you can read your xml to the datset. You will get 5 datatables:
DCSubtitles
Font1
Font
Subtitle
Text

Now you can read every datatable to get the required info

here is an example to show you what I mean
Keep in mind that I called your xml - info.xml and put it into bin directory


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents dg As System.Windows.Forms.DataGrid
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.dg = New System.Windows.Forms.DataGrid
        Me.Button1 = New System.Windows.Forms.Button
        CType(Me.dg, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'dg
        '
        Me.dg.DataMember = ""
        Me.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dg.Location = New System.Drawing.Point(0, 0)
        Me.dg.Name = "dg"
        Me.dg.Size = New System.Drawing.Size(556, 172)
        Me.dg.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 184)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(636, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.dg)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.dg, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ds As New DataSet

        ds.ReadXml("info.xml")
        dg.DataSource = ds
    End Sub
End Class



0
 
davidogdenblueAuthor Commented:
wow fast response thanks!

What should I be declaring "dg" as?
0
 
davidogdenblueAuthor Commented:
Sorry I ignored the code that you seemed to have commented out. Whoops
0
 
iboutchkineCommented:
dg is a datagrid name. Paste the code and you can run it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.