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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search Strings and Work w/ XML in C# 2

Status
Not open for further replies.

natwod

Programmer
Nov 21, 2002
41
US
Ok, a couple questions here. I've searched the documentation, but my documentation isn't very good.

Basically, I am converting a text-based file type into an XML-based format, and vice versa.

I am using a StreamReader and ReadToEnd() to get the entire text file into a String variable. BTW, is there a better way to do that, like ReadLine and store each line in an array? I tried, but couldn't figure it out.

I also can't figure out how to search a string and output the equivalent XML command. Are there any String-search methods buried somewhere in a library class?

Thirdly, I'm sure that C# has a bunch of XML classes and methods, but I can't find them. How does one read in an XML file, navigate through the DOM, and also output an XML file?

I know this is a lot of questions, but any replies are GREATLY appreciated.

Thanks very much!

Natwod

 
This is something I knocked here for string processing - hope it helps.



using System;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class File_Parse_and_Update
{
/// <summary>
/// Uses System.IO
/// This procedure will run through a given directory and will manipulate files
/// according to whether or not it finds the search criteria.
/// In this case, if it finds the exii schema used, it will add the supplied database link
/// to source the data from a new location.
/// All files are written out, then deleted if they have not been changed.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// string sourcepath = @"\\Teach_43\E\bis\database\create\Packages\bis_dbo\";
// string sourcepath = @"\\Hqexidb01p\exii\database\create\tps_dbo\packages\";
string sourcepath = @"U:\test\";
string destpath = @"U:\My_Documents\BIS Changes\";
string SearchString = "exii.";
string ReplaceString = "exii.";
string AddString = "@report1.world";
bool file_altered;
bool write_old_source;
int pos = 0;

string line;

DirectoryInfo dir = new System.IO.DirectoryInfo(sourcepath);
FileInfo[] filesInDir = dir.GetFiles();
foreach(FileInfo sourcefile in filesInDir)
{
// Process the files in the directory
StreamReader infile = new StreamReader(sourcepath + sourcefile);
StreamWriter outfile = File.CreateText(destpath + sourcefile);
Console.WriteLine("processing " + sourcefile);

file_altered = false;
line = infile.ReadLine();
while(line != null)
{
if (line != "")
{
// not comment lines ?
if (line.Substring(0,1) != "|" &&
line.Substring(0,1) != "-")
{
write_old_source = true;
pos = line.IndexOf(SearchString);
while(pos >= 0)
{
file_altered = true;
if (write_old_source)
{
outfile.WriteLine("--GRB " + DateTime.Now.ToString("d") + " " + line);
write_old_source = false;
}
//replace string processing
line = line.Substring(0,pos) + ReplaceString + line.Substring(pos + SearchString.Length);
//position to end of column reference
while (pos < line.Length &&
line.Substring(pos,1) != " " &&
line.Substring(pos,1) != "'" &&
line.Substring(pos,1) != ")" &&
line.Substring(pos,1) != "," &&
line.Substring(pos,1) != ";" &&
line.Substring(pos,1) != "'" )
{
pos++;
}
line = line.Substring(0,pos) + AddString + line.Substring(pos);

pos = line.IndexOf(SearchString, pos + AddString.Length);
}
}
}
outfile.WriteLine(line);
line = infile.ReadLine();
}

infile.Close();
outfile.Close();
if (!file_altered)
{
Console.WriteLine("deleting " + sourcefile);
Console.WriteLine(" ");
File.Delete(destpath + sourcefile);
}
}
Console.WriteLine("Processing Finished");
Console.ReadLine();

}
}
}


Regards,
Graham
 
Add this to it and you get the idea


XmlDocument requestXML = new XmlDocument();

//Create the root request node
XmlElement rootNode = requestXML.CreateElement("fred","Illustration"," requestXML.AppendChild(rootNode);

XmlNode requestNode = requestXML.CreateElement("Request");
rootNode.AppendChild(requestNode);

XmlNode childNode = requestXML.CreateElement("IllustrationDate");
childNode.InnerText = DateTime.Parse(this.com_IllustrationStartDateYear.Text + "-" + m_MonthDescription[this.com_IllustrationStartDateMonth.SelectedIndex].ToString() + "-" + this.com_IllustrationStartDateDay.Text).ToString("yyyy-MM-dd");
requestNode.AppendChild(childNode);

childNode = requestXML.CreateElement("PolicyStartDate");
childNode.InnerText = DateTime.Parse(this.com_PolicyStartDateYear.Text + "-" + m_MonthDescription[this.com_PolicyStartDateMonth.SelectedIndex].ToString() + "-" + this.com_PolicyStartDateDay.Text).ToString("yyyy-MM-dd");
requestNode.AppendChild(childNode);

childNode = requestXML.CreateElement("SalesChannel");
childNode.InnerText = this.cbo_SalesChannel.SelectedValue.ToString();
requestNode.AppendChild(childNode);


Regards,
Graham
 
It is not clear what exactly text file has to be converted into another text file but formatted as an XML file.
I think your text file contains some data that could be mapped to a table e.g. looking for some tags and values.
So I will try the following solution:
- Define a DataSet object let be it ds for reference. (DataSet ds = new DataSet("myDataSe");)
- Add one or more tables in ds and defines columns depending on the record structure of the text file
- open input text file
- for each line read :
. parse the line using string.Find() or regular expressions to have something
. add a record in a DataTable of the ds object
- close file
- ds.WriteXml("myfile.xml");

To read an xml file you can also load it into a DataSet object by only: ds.ReadXml("mytext.xml");
You can iterate throug data using the DataSet facilities, modify it and finally you could change the "mytext.xml" by
only ds.WriteXml("mytext.xml");
Another method is to load the "mytext.xml" into XmlDocument as is in the previous answer.
-obislavu-
 
Thanks for the great posts. These'll really help me out

natwod

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top