data from CSV + Rules XSD -> XML

G

Guest

I am writing a C# program to to convert CSV to XML based on an XSD schema file.

I have a CSV that could contain 3 different types of lines that is
identified by the first field of data Assume that the 3 types are A,B,C

A,foo,bar,1,2
B,barr,4,5
C,foox,1
A,bar,foo,1,2,3
....

So a message that starts with
A ...has.... 5 fields
B ...has.... 4 fields
C ...has.... 3 fields

My XSD would be something like

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="BLAA" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="A">
<xs:sequence>
<xs:element name="colA1" type="xs:string" />
<xs:element name="colA2" type="xs:string" />
<xs:element name="colA3" type="xs:int" />
<xs:element name="colA4" type="xs:int" />
</xs:sequence>
</xs:group>
<xs:group name="B">
<xs:sequence>
<xs:element name="colB1" type="xs:string" />
<xs:element name="colB2" type="xs:int" />
<xs:element name="colB3" type="xs:int" />
</xs:sequence>
</xs:group>
<xs:group name="C">
<xs:sequence>
<xs:element name="colC1" type="xs:string" />
<xs:element name="colC2" type="xs:int" />
</xs:sequence>
</xs:group>
</xs:schema>

Final XML output would be somthing like


<data>
<ROW type="A">
<colA1>foo</colA1>
<colA2>bar</colA2>
<colA3>1</colA3>
<colA4>2</colA4>
</ROW>
<ROW type="B">
<colB1>barr</colB1>
<colB2>4</colB2>
<colB3>5</colB3>
</ROW>
<ROW type="C">
<colC1>foox</colC1>
<colC2>1</colC2>
</ROW>
......

</data>

Of course my C# will know that when you read the CSV and find the first word
to be A is to go to look for the A "type" in the XSD and know the following
sequence.
in http://www.creativyst.com/Prod/15/ they are optaining the
COL[A|B|C][1..n] from a form input. I want to load it from an XSD.
My question i guess finally would be
- Since I will parse the data based on an XSD, Is there a specific API for
reading XSD or just use XPath as if i am reading a normal XML? None of the
classes in System.Xml.Xsl seemed to do that sort of parsing.
-Any body with a better idea out there?? :)

Thanks aloT!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top