Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I've got to write an application that will read in a text file then output the contents of that file as a new XML file.

This is a snippet of the text file I have to work with. The structure of the text file will always follow the same pattern and structure; only the values will change...

order.contact.email#bob@bob.com
order.contact.tel#0123 4567890
order.contact.fax#0123 4567891
order.enquiry#ABC1234
order.total-price#0.00
order.subtotal-price#0.00
order.total-tax#0.00
order.id#XYZ987


I've read that into a <list> using StreamReader, it contains 56 lines in total thinking that I can loop through and assign the relevant value to the relevant XML node.

As you can see, the value for each node follows the # in each line. I've generated an empty XML skeleton (order.xml) for each node and also the XSD schema (order.xsd) using VS2005's XSD tool.

There may be areas in the resulting XML file which need to repeated such as one or more single PART in a PARTS group where PART has various properties such as

...
   <PARTS>
     <PART>
       <MATERIAL>Material 1</MATERIAL>
       <NAME>Name 1</NAME>
     </PART>
     <PART>
       <MATERIAL>Material 2</MATERIAL>
       <NAME>Name 2</NAME>
     </PART>
    </PARTS>
...

I've capitalized each node as I can't include the angled brackets, apologies if you find it hard to read.

What I can't figure out is how to tie the whole thing together. Please can someone point me in the direction of how to build this?

With best regards

Scott
Posted
Updated 21-Jan-11 3:48am
v3
Comments
#realJSOP 21-Jan-11 8:34am    
If you edit your question, you'll see how to escape the pointy-bracket characters so they show up. Also, learn about the <pre> tag - it will keep your code snippet formatting.
#realJSOP 21-Jan-11 9:49am    
I changed your question tags to reflect that you're using .Net 2.0.

There are a number of ways to get the data into XML, and Xml serialization would probably be the easiest way, assuming that when you read your data in from the text file, you're creating objects, and not just trying to go right from the string values to XML.

When I have to do something like this, I create an object to hold the data, and provide a XElement property that allows me to set/get the object data using a XElement object. This makes it easy to maintain, and keeps all the nigly xml stuff localized to a good degree. he secret to my method is this:

using System.Xml.Linq;

The XDocument, XElement, and XAttribute objects really make doing XML easy. I also wrote a tip/trick that describes some extension methods I came up with to make getting/setting values easier:

Using Extension Methods To Avoid XML Problems[^]

EDIT ===============

You're right, .Net 2.0 doesn't have Linq. There should be a lot of XML helper classes available out there in the ether. Some judicious googling on your part would probably go a long way toward finding an answer. many times, google will expose applicable articles right here on CodeProject.
 
Share this answer
 
v3
Comments
CPallini 21-Jan-11 9:16am    
[Carlo on behalf of scotlandc] Thanks for the reply, and also showing me how to add the tags to my initial post, much appreciated.

I was going straight from the strings to XML I must admit, however, I will go about adding the appropriate objects now.

However, with that being said, I'm only on .Net 2.0 here and VS 2005 so using Linq is not an option I don't think. Please correct me if I'm wrong. Am I still able to use your advice given this possible constraint?
hey ,
another easy way in which you will not have to worry about the schema is that loop through the record and add the records to a datatable rows. and then simply call the
Datatable.WriteXML("C://test.xml"); 

method. it will write the XML file on the specified location.
Best of luck
Ahsan Sarfraz
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900