Mega Code Archive

 
Categories / Delphi / Files
 

Using XmlTextReader to read an XML file in Delphi 8

Title: Using XmlTextReader to read an XML file in Delphi 8 Question: How do you read an XML file in Delphi 8 using the Framwork XmlTextReader class? Answer: We will write a simple Windows File Application to read and XML file and display it in a list box. First we need to create an XML file to read. You can do this in notepad or Delphi create a file containing the following and save it as name.xml: Tom Smith Now lets create our simple program to read an xml file Select File = WinForms Forms Application Your new form appears in the forms designer. Press F11 in the Object Inspector change the text to Simple XML Reader. Press Alt+Ctrl+P this brings up the tool palette (1) Select the Windows Forms tab. (2) Drop listbox and a Button on the form. (3) Select the Dialog tab. (4) Drop an openfiledialog on the form (5) Line up the listbox on the top of the form. (6) Place the button below Listbox1 and change the text to add. procedure TWinForm.InitializeComponent; begin // // Button1 // Self.Button1.Location := System.Drawing.Point.Create(96, 344); Self.Button1.Name := 'Button1'; Self.Button1.TabIndex := 1; Self.Button1.Text := 'Button1'; Include(Self.Button1.Click, Self.Button1_Click); // // ListBox1 // Self.ListBox1.Location := System.Drawing.Point.Create(40, 40); Self.ListBox1.Name := 'ListBox1'; Self.ListBox1.Size := System.Drawing.Size.Create(208, 264); Self.ListBox1.TabIndex := 2; // end; Add these namespaces to the uses clause of your unit System.Data, System.Xml Double click on button1 on the form and add the following code procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs); var str_filename : string; objTxtRd : XmlTextReader; begin OpenFileDialog1.ShowDialog(); str_filename := OpenFileDialog1.FileName; ListBox1.Items.Clear; objTxtRd := XmlTextReader.Create(str_filename); while objTxtRd.Read do begin case objTxtRd.NodeType of XMLNodeType.Text:ListBox1.Items.Add(objTxtRd.Value); XMLNodeType.Whitespace:ListBox1.Items.Add(objTxtRd.NodeType); else listBox1.Items.Add(objTxtRd.Name + ' is type ' + System.Convert.ToString(objTxtRd.NodeType)); end; end; end; Now lets look at what this simple example is doing. First we create an instance of XmlTextReader named objTxtRd. In the constructor we pass a string containing the location and name of the xml file we will be reading. Then we simply loop thru all the lines in the xml file by doing an objTxtRd.Read. The program then checks to see if the NodeType is Text. If it is we place the value of the line in the list box. The program then checks to see if the NodeType is whitespace. If it is we place the word whitespace in the list box. If the NodeType is not whitespace or text it puts the value of the node followed by its type in the listbox. Now lets run our example and click on button and select the name.xml from the dialog that appears and click OK. The listbox in you application should now contain the following information. xml is type xmldeclartion whitespace Name is type element Whitespace First is type element Whitespace Tom First is type Endelement Whitespace Last is type element Smith Last is type EndElement Whitespace name is type element whitespace In name.xml the white space really has no meaning or significance. So we can have the reader remove the whitespace for us. By default the XmlTextReader sets its whitespacehandling property to ALL. The valid values from the help file for this property are: ALL = Return Whitespace and SignificantWhitespace nodes. This is the default None = Return no Whitespace and no SignificantWhitespace nodes. Significant = Return Significant Whitespace nodes only Now we will change our example to remove the whitespace. Add the following line after the line where we create objTxtRd: objTxtRd.WhitespaceHandling := WhitespaceHandling.None; So our code will look as follows: objTxtRd := XmlTextReader.Create(str_filename); objTxtRd.WhitespaceHandling := WhitespaceHandling.None; while objTxtRd.Read do Now lets run our example and click on button and select the names.xml from the dialog that appears and click OK. The listbox in you application should now read the xml file without whitespace xml is type xmldeclartion Name is type element First is type element Tom First is type Endelement Last is type element Smith Last is type EndElement name is type element