By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,013 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,013 IT Pros & Developers. It's quick & easy.

Convert flat file to XML document

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.