473,396 Members | 1,608 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,396 software developers and data experts.

Comma delimited data conversion to XML

Is there an easy way to convert a string of data to XML format without having
to create the xml one field at a time?

Sorry I'm a newbie and my xml knowledge is limited. I have tried to search
through the documentation in .net but nothing jumps out at me.

Sample code would be great!
Nov 12 '05 #1
2 1985
"Larry Williams" <Larry Wi******@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
Is there an easy way to convert a string of data to XML format without having
to create the xml one field at a time?


Certainly, but the easier the approach you take, the less meaningful structure your
XML document is likely to have.

First, suppose you have the string of data (strData), "03040517761492oceanblue".
This string can easily turned into an XML document,

XmlDocument doc = new XmlDocument( );
doc.LoadXml( string.Format( "<pwd>{0}</pwd>", strData));

Nothing difficult about that, but nothing "smart" about it either. The one-step
process yields you with a one-element XML document using only .NET's
standard string formatting (moreso than any System.Xml functionality.)

For the next example assume your data is comma-delimited, such as,

string strData = "mary,kate,ashley,olson";

This time you can use the Replace( ) function to inject closing and opening tags at
the positions within the string where there are commas now, and then wrap them
in an initial opening and a concluding closing tag.

XmlDocument doc = new XmlDocument( );
doc.LoadXml( string.Format( "<list><name>{0}</name></list>", strData.Replace( ",", "</name><name>")));

This leads to the XML instance document (when printed with indented formatting),

<list>
<name>mary</name>
<name>kate</name>
<name>ashley</name>
<name>olson</name>
</list>

However, what you'll find with these approaches is that the more structure you want
to impose, the greater the need is for you to operate on a field-by-field basis. For
instance, you can't escape at least examining each individual string token in the above
example were your XML document required to hold sufficient information to distinguish
between the Olson Twins' given and last names,

string[] names = strData.Split( new char[] { ',' });
System.Text.StringBuilder buf = new System.Text.StringBuilder( );
buf.Append( "<list>");
foreach( string name in names)
buf.AppendFormat( "<{0}>{1}</{0}>", ( ( 5 == name.Length ) ? "lastName" : "firstName" ), name);
buf.Append( "</list>");

XmlDocument doc = new XmlDocument( );
doc.LoadXml( buf.ToString( ));

This converts the string of comma-delimited data into the following XML document,

<list>
<firstName>mary</firstName>
<firstName>kate</firstName>
<firstName>ashley</firstName>
<lastName>olson</lastName>
</list>

Before we start arguing over whether Mary Kate is one first name or two; :-) the
amount of effort necessary is application-dependent. There's no silver bullet that
brings into existence an ordered structure out of thin air -- one that turns data into
information.
Derek Harmon
Nov 12 '05 #2
Larry Williams wrote:
Is there an easy way to convert a string of data to XML format without having
to create the xml one field at a time?


If it's CSV data, you can read it as XML with XmlCsvReader:
http://www.gotdotnet.com/Community/U...B-57A7DBBEBAE0

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #3

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

Similar topics

4
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous...
4
by: Ron McCafferty | last post by:
How do you read comma delimited data from programs such as Excel where the data itself contains dounble quotes and commas. It used to be you could just use input. How is it fone now? The data...
3
by: Elmo Watson | last post by:
I've been asked to develop a semi-automated type situation where we have a database table (sql server) and periodically, there will be a comma delimited file from which we need to import the data,...
1
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
9
by: Wayne | last post by:
I have the following string: "smith", "Joe", "West Palm Beach, Fl." I need to split this string based on the commas, but as you see the city state contains a comma. String.split will spilt the...
5
by: Yama | last post by:
Hi, I am looking to create a report comma delimited on a click of a button. Explanantion: 1. Get from the database: "SELECT * FROM Customers WHERE Region = 'CA'" 2. Use either DataReader or...
3
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
15
by: VMI | last post by:
I'm parsing a comma-delimited record but I want it to do something if some of the string is between "". How can I do this? With the Excel import it does it correct. I'm using String.Split()....
5
by: bebe | last post by:
Hi, How do I write a function in SQL to convert data from a table to a comma delimited text file and get it imported in excel or word? I already have a function that convers data to an xml. but I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.