Reputation: 1937
I am playing with Fsharp script and trying to find a way to convert data from an XML file to a CSV file.
I started by reading the XML file with typeprovider, what I wanted after that is to use ServiceStack.Text serializer but of course the data are not ready for it as I still have an XmlProvider.
I am probably appraoching the problem from the wrong side. Any insight ?
Upvotes: 2
Views: 224
Reputation: 243106
If you wanted to use XML and CSV type providers, you could define two types - one to represent your input based on a sample and one to represent your output based on an explicit schema.
Assuming test.xml
looks like this:
<data>
<item foo="1" bar="2" />
<item foo="1" bar="3" />
</data>
You can then define the types as below. For the Output
type, we're saying what are the column names and their types for the output CSV:
open FSharp.Data
type Input = XmlProvider<"c:/temp/test.xml">
type Output = CsvProvider<"foo (int), bar (int)">
Now you can turn the input data into output rows, add them to a new empty CSV file and save it to a string using SaveToString
or to a file using just Save
:
let newRows =
[ for r in Input.GetSample().Items ->
Output.Row(r.Foo, r.Bar) ]
Output.GetSample().Append(newRows).SaveToString()
Upvotes: 5