473,511 Members | 13,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transform CSV files to XML

Below is a quick-and-dirty class to do your conversion.
It requires tabular data and assumes the first line of the
CSV stream is the list of field names. This should get
you going.

Here's how you'd call it.

StreamReader sr = new StreamReader( "test.csv" );
CsvToXml c = new CsvToXml( sr, Console.Out );
....

class CsvToXml
{
public CsvToXml ( TextReader csvIn, TextWriter xmlOut)
{
string ln;
bool bHeader = true;
string[] aFields = new string[] { }, aData;

while (null != (ln = csvIn.ReadLine()))
{
if (ln.Trim().Length < 1) continue; // handle empty
lines
if (bHeader)
{
bHeader = false;
aFields = this.splitLine( ln );
for (int i = 0; i < aFields.Length; i++)
{
if (Regex.Replace( aFields[i],
"[A-Za-z]([A-Za-z0-9._] | '-')*",
"" ).Length > 0)
throw new ApplicationException(
"Field cannot be converted to an NCNAME - "
+ aFields[i] );
aFields[i] = aFields[i].Replace( " ", "_" );
}
xmlOut.WriteLine( "<data>" );
}
else
{
aData = this.splitLine( ln );
if (aData.Length != aFields.Length) throw new
ApplicationException( "Invalid data encountered -
expected "+ aFields.Length.ToString() + " fields: "+ ln );
xmlOut.WriteLine( this.mergeFieldValues( aFields,
aData ) );
}
}
xmlOut.WriteLine( "</data>" );
}

private string[] splitLine( string line )
{
string[] a = line.Split( new Char[] {','} );
for (int i = 0; i < a.Length; i++)
a[i] = Regex.Replace( a[i], "(^['\"])|(['\"]
$)", "" ); // remove any enclosing quotes
return a;
}
private string mergeFieldValues( string[] fields, string
[] values )
{
StringBuilder sb = new StringBuilder( fields.ToString
().Length );
for (int i = 0; i < fields.Length; i++)
{
sb.Append( " " + fields[i] + "=\""+
values[i].Replace( "\"", "&#x22;" ).Replace
( "<", "&lt;" ).Replace( ">", "&gt;" ) + "\"" );
}
sb.Append( "/>" );
sb.Insert( 0, "<row" );
return sb.ToString();
}
}
-----Original Message-----
I need to convert CSV (comma delimited files) to XML on
the fly for FTP. I only found references to XML / Pearl,
and I would like to find out references compatible with
microsoft... I appreciate any leads :) thanks.
.

Nov 11 '05 #1
1 2915


Thanks - I also found that ActivePearl PPM component utilizes XML::CSV
as a way to create XML documents from CSV files. We are ready to install
ActivePearl and check it out. Thanks again.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 11 '05 #2

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

Similar topics

1
2236
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
2
8785
by: Showjumper | last post by:
I've got the following code. But the XSLTransform line is underlined by the blue squiggle and vs tells me c:\inetpub\wwwroot\Sites\RiderDesign\articles\test.aspx.vb(37): 'Public Sub...
3
2356
by: Showjumper | last post by:
I need to do a transform using 2 xsl files? How would i go about this? This is what am doing now - just a single transform. Dim XMLDoc As New XmlDocument...
6
2469
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
4
4806
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
6
1708
by: tcdevelopment | last post by:
I have a XSLT file that gives expected results when I transform using MSXML V4.0 in a simple vbs file. However when using XslTransform in dotnet, I do not get the same results. The part of the...
9
1565
by: Doug Stiers | last post by:
I have this vb.net (framework 1.1) code: Dim x As Xml.Xsl.XslTransform = New Xml.Xsl.XslTransform Dim xr As XmlResolver MessageBox.Show("before load") x.Load(<xsl file name>) -- THIS IS WHERE...
3
302
by: Peter Row | last post by:
Hi, I have 2 XML files and 1 XSLT file. The second XML file has the following declarative 1st line: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> ....the 1st one (the one to be...
12
2876
by: das | last post by:
Hello all, I am using .NET XSLT to transform an XML into another XML file. All this is fine with small files, but when tested with big files (30MB) it is taking between 1hr-2hrs to just transform...
2
2062
by: =?Utf-8?B?S2lt?= | last post by:
HowHi all I want to transform data in xml files to *.txt in a C# application. I have some data in Xml files and want to used them in a 3D tool to print the subject I know little about XSLT, so...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.