Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML File Parsing

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello, i need to write a console app that will search a folder and subfolder for 1 or more files of a given filename. for each file that is found, the XML file must be parsed for a substring in the <url> element, and replace it w/ another substring. in other words, for each <url> tag found in the XML file i am using (the name of the file is TNT.XML), search for a substring and replace it w/ another. I would appreciate any help. Thanks in advance.
 
how about the XmlReader and XmlWriter classes? The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
i need to use the system.xml and system.io reference libraries that can be linked to the c# console.
 
If all you're doing is just replacing text in the found file, why don't you open the file and use a regex replace function ? Probably loads faster than parsing the file manually through specific elements.
 
I have the searching part done, can anyone help with changing the url in the XML file? Code would be helpful. Here is the searching code from ms.com.
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace RecursiveSearchCS
{
   
    public class Form1 : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Button btnSearch;
        internal System.Windows.Forms.TextBox txtFile;
        internal System.Windows.Forms.Label lblFile;
        internal System.Windows.Forms.Label lblDirectory;
        internal System.Windows.Forms.ListBox lstFilesFound;
        internal System.Windows.Forms.ComboBox cboDirectory;
     
        private System.ComponentModel.Container components = null;

        public Form1()
        {
         
            InitializeComponent();
          
        }

            protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
      
        private void InitializeComponent()
        {
            this.btnSearch = new System.Windows.Forms.Button();
            this.txtFile = new System.Windows.Forms.TextBox();
            this.lblFile = new System.Windows.Forms.Label();
            this.lblDirectory = new System.Windows.Forms.Label();
            this.lstFilesFound = new System.Windows.Forms.ListBox();
            this.cboDirectory = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
         
            this.btnSearch.Location = new System.Drawing.Point(608, 248);
            this.btnSearch.Name = &quot;btnSearch&quot;;
            this.btnSearch.TabIndex = 0;
            this.btnSearch.Text = &quot;Search&quot;;
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
        
            this.txtFile.Location = new System.Drawing.Point(8, 40);
            this.txtFile.Name = &quot;txtFile&quot;;
            this.txtFile.Size = new System.Drawing.Size(120, 20);
            this.txtFile.TabIndex = 4;
            this.txtFile.Text = &quot;*.dll&quot;;
          
            this.lblFile.Location = new System.Drawing.Point(8, 16);
            this.lblFile.Name = &quot;lblFile&quot;;
            this.lblFile.Size = new System.Drawing.Size(144, 16);
            this.lblFile.TabIndex = 5;
            this.lblFile.Text = &quot;Search for files containing:&quot;;
         
            this.lblDirectory.Location = new System.Drawing.Point(8, 96);
            this.lblDirectory.Name = &quot;lblDirectory&quot;;
            this.lblDirectory.Size = new System.Drawing.Size(120, 23);
            this.lblDirectory.TabIndex = 3;
            this.lblDirectory.Text = &quot;Look In:&quot;;
          
            this.lstFilesFound.Location = new System.Drawing.Point(152, 8);
            this.lstFilesFound.Name = &quot;lstFilesFound&quot;;
            this.lstFilesFound.Size = new System.Drawing.Size(528, 225);
            this.lstFilesFound.TabIndex = 1;
        
            this.cboDirectory.DropDownWidth = 112;
            this.cboDirectory.Location = new System.Drawing.Point(8, 128);
            this.cboDirectory.Name = &quot;cboDirectory&quot;;
            this.cboDirectory.Size = new System.Drawing.Size(120, 21);
            this.cboDirectory.TabIndex = 2;
            this.cboDirectory.Text = &quot;ComboBox1&quot;;
        
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(688, 277);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {

            this.btnSearch,
            this.txtFile,
            this.lblFile,
            this.lblDirectory,
            this.lstFilesFound,
            this.cboDirectory});

            this.Name = &quot;Form1&quot;;
            this.Text = &quot;Form1&quot;;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            lstFilesFound.Items.Clear();
            txtFile.Enabled = false;
            cboDirectory.Enabled = false;
            btnSearch.Text = &quot;Searching...&quot;;
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();
            DirSearch(cboDirectory.Text);
            btnSearch.Text = &quot;Search&quot;;
            this.Cursor = Cursors.Default;
            txtFile.Enabled = true;
            cboDirectory.Enabled = true;
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            cboDirectory.Items.Clear();
            foreach (string s in Directory.GetLogicalDrives())
            {
                cboDirectory.Items.Add(s);
            }
            cboDirectory.Text = &quot;C:\\&quot;;
        }

        void DirSearch(string sDir) 
        {
            try	
            {
                foreach (string d in Directory.GetDirectories(sDir)) 
                {
                    foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
                    {
                        lstFilesFound.Items.Add(f);
                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt) 
            {
                Console.WriteLine(excpt.Message);
            }
        }
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top