473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Convert flat file to XML document

Hi,

I am a newbee and have a comma seperated flat-file and a DTD. I need to
write a C# program which will read the text file and convert it to a
XML file as per the the definition in the DTD. I am not able to find
anything help-ful and don't know how to begin to code.

Can anyone please help me?

Thanks in advance

Ashoo Sharda

Dec 19 '05 #1
8 14558
It sounds like you're going to have to write code to read that file into a
class with the structure you want. Depending on the requirements on the
format, you may be able to make the class that holds the data [Serializable]
and then just stream it out to disk (couple lines of code). Worst case, if
the XML structure won't match up with a serializable class, you'll have to
dig into the XML namespaces and it can definately be done there.

Craig

"asrs63" <as**********@reyrey.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,

I am a newbee and have a comma seperated flat-file and a DTD. I need to
write a C# program which will read the text file and convert it to a
XML file as per the the definition in the DTD. I am not able to find
anything help-ful and don't know how to begin to code.

Can anyone please help me?

Thanks in advance

Ashoo Sharda

Dec 19 '05 #2
Hi,

Thanks a lot for your quick response, but I am looking for a more
generic solution so I can use the same code to convert different
flat-files with different DTD definitions, also I have a lot of hirarchy
which means I would have lot of classes to maintain.

Thanks,
Ashoo Sharda

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 20 '05 #3
KJ
Of course we can help. First off, you have many options as to how to go
about this. One approach I might take is to use the System.Data.OleDb
classes to read the records from the flat file into a DataSet, and then
iterate through that, while writing to an XML file using the
XmlTextWriter. You might find need to use the System.Text.Regex class
to parse your data. Finally, you can use the XmlValidatingReader to
read your Xml to make sure it's valid (Cf. MSDN "How To Validate an XML
Document by Using DTD, XDR, or XSD in Visual C# .NET").

Dec 20 '05 #4
On 19 Dec 2005 14:39:51 -0800, "asrs63" <as**********@reyrey.com>
wrote:
Hi,

I am a newbee and have a comma seperated flat-file and a DTD. I need to
write a C# program which will read the text file and convert it to a
XML file as per the the definition in the DTD. I am not able to find
anything help-ful and don't know how to begin to code.

Can anyone please help me?

Thanks in advance

Ashoo Sharda


How much of a newbie are you? I will assume that you don't know much,
my apologies of I am wrong.

Most newbies make the mistake of trying to write too much at once.
Avoid this by writing a little and then testing it. Only go on when
all the tests work.

Start by writing a smell program to open a file and copy its contents
to the screen. Yes, that simple. It is something you will need to do
as part of your overall program so you might as well start with that.
Test it with different test files to check that it works.

Now add to your program so that it writes a second copy of the
original file to disk, with a new name. Again this is something you
will need to do in your overall program. Test it and check that you
are indeed making a copy of the input file.

Read your C# and XML books so you understand what C# can offer you as
built-in XML handling as part of .NET

Now try a very simple DTD and equivalent CSV file. Read in the DTD
and the CSV file, produce the XML file. Check that the XML file is
what you expect.

As required add extra processing to your program so that you can
process more and more complex DTD/CSV files. After each incremental
improvement test to make sure that your program still works correctly.

If you get stuck then post what you have written so we can have a look
at it. It lets us help you more of we can see your code.

rossum

--

The ultimate truth is that there is no ultimate truth
Dec 20 '05 #5
Thanks for all your help. I have managed to write the code to generate
an XML file from a data file given the DTD.

Thanks,
Ashoo.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 20 '05 #6
Ashoo, can you please send me your solution, because I'm in urgent need
te do the same type of conversion.

Thanx in advance.

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 23 '05 #7
namespace createXMLfile
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;

private System.ComponentModel.Container components = null;
private Hashtable dataHash ;
private Hashtable strucHash ;
private Stack tokensStack = new Stack();
private Stack tagsStack = new Stack();
private XmlDocument doc = new XmlDocument();
private void CreateXMLDoc(string tag)
{
XmlNode pr_node ;

string str ;

pr_node = doc.CreateElement(tag);
doc.AppendChild(pr_node);

if (strucHash.ContainsKey(tag))
{
str = strucHash[tag].ToString();

string delim = "|";

char [] delimiter = delim.ToCharArray();

string [] children = str.Split(delimiter);
XmlNode child;

for ( int i = 0; i < children.Length; i++)
{
string child_name = children[i];

try
{
if (child_name.Equals(""))
return;

child = doc.CreateElement(child_name);
pr_node.AppendChild(child);
if (dataHash.ContainsKey(child_name))
{
child.InnerText = dataHash[child_name].ToString();
}
if (strucHash.ContainsKey(child_name) && !child_name.Equals(""))
{
CreateXMLDoc(child, child_name);
}
}
catch (Exception ex)
{
WriteToTextBox(ex.ToString() + "\n");
}

}
}
}
private void CreateXMLDoc(XmlNode pr_node, string tag)
{
string str;

if (strucHash.ContainsKey(tag))
{
str = strucHash[tag].ToString();

string delim = "|";

char [] delimiter = delim.ToCharArray();

delim = "0123456789";

char [] num_arr = delim.ToCharArray();

string [] children = str.Split(delimiter);
XmlNode child;
string num = "";

for ( int i = 0; i < children.Length; i++)
{
string child_name = children[i];

if (child_name.IndexOfAny(num_arr) > 0)
{
int index = child_name.IndexOfAny(num_arr);
int len = child_name.Length ;

num = child_name.Substring(index, len-index);

child_name = child_name.Substring(0, index);
}

try
{
if (child_name.Equals(""))
return;

child = doc.CreateElement(child_name);
pr_node.AppendChild(child);

if (dataHash.ContainsKey(child_name + num))
{
child.InnerText = dataHash[child_name + num].ToString();
}
if (strucHash.ContainsKey(child_name + num) &&
!child_name.Equals(""))
{
CreateXMLDoc(child, (child_name + num));
}

}
catch (Exception ex)
{
WriteToTextBox(ex.ToString());
}

}
}
}

private void CreateTextFile(string beginTag)
{
XmlTextWriter writer = new XmlTextWriter("data.xml",null);

writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(false);
writer.WriteDocType(beginTag, null, "file:///c:\filename.dtd", null);
writer.WriteComment("This file represents Vehicle Inventory
database");

doc.Save(writer);

}

private void WriteToTextBox(string text)
{
richTextBox1.AppendText(text);
}

private string Read_DTD(string filename)
{
strucHash = new Hashtable();
string beginTag = null;

StreamReader reader = null;
string line = null;
string delim = ",";
bool continueLine = false;
bool tagFound = false;

string completeLine = "";
char [] delimiter = delim.ToCharArray();

try
{

reader = new StreamReader(filename);

while ((line = reader.ReadLine()) != null)
{
if (line.IndexOf("<!ELEMENT") >= 0 && line.IndexOf("PCDATA") < 0)
{
continueLine = true;

line.Replace("\n", " ");

completeLine = completeLine + line ;

WriteToTextBox(line + "\n");
}
else if (continueLine )
{
line.Replace("\n", " ");
completeLine = completeLine + line ;

if (line.IndexOf(">") >= 0)
{
continueLine = false;

WriteToTextBox("Complete Line = " + completeLine + "\n\n");

completeLine = completeLine.Replace("<!ELEMENT", "");
completeLine = completeLine.Replace(",", "|");
completeLine = completeLine.Replace("(", ",");
completeLine = completeLine.Replace("+", "|");
completeLine = completeLine.Replace("*", "|");
completeLine = completeLine.Replace(")", "");
completeLine = completeLine.Replace(">", "");
completeLine = completeLine.Replace(" ", "");

if (completeLine.EndsWith("|") )
{
completeLine = completeLine.Substring(0, completeLine.Length -
1);
}

string [] line_tokens = completeLine.Split(delimiter);

if ( !tagFound)
{
beginTag = line_tokens[0];
tagFound = true;
}
completeLine = null;
if (!line_tokens[0].Equals(""))
strucHash.Add(line_tokens[0], line_tokens[1]);

}
}
}
}
catch (Exception ex)
{
WriteToTextBox(ex.ToString());
}

return beginTag;

}

private void Form1_Load(object sender, System.EventArgs e)
{

dataHash = new Hashtable();
strucHash = new Hashtable();
string beginTag = "";

beginTag = Read_DTD("filename.dtd");

WriteToTextBox("Begin tag returned is " + beginTag + "\n\n");
CreateXMLDoc(beginTag);
CreateTextFile(beginTag) ;

}
}
}
Here is what I wrote. You might need to fine tune it. If you get a
better more efficient solution please do send me also. dataHash is the
hash table which will contain the data tags as keys and the values as
the value to be contained in the data. Also, I am yet not sure as to how
I am going to handle multiple tags. unless u put a number next to the
tag it will work. example

person, person1, person2 it will work.

Due to shortage of space I have deleted the windows methods

Thanks,
Ashoo

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 25 '05 #8
If your target XML is more specific, then you may find the XML Data
Binding approach more useful

Have a look at http://www.liquid-technologies.com/Download.aspx

Regards Simon

Jan 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to...
13
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
29
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL...
3
by: R. P. | last post by:
Subject: XSLT to transform a flat XML file into a structured text file I have an XML file that lists the PDF file segment names and titles of a larger document and looks something like this: ...
11
by: JimN1 | last post by:
Hi, I am looking for an example to convert a flat text file with variable length records to a flat file with fixed length records. Does anyone have an example?
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.