F # Xml TypeProvider for C #

Is there an equivalent to F # TypeProvider in C #? I am looking at the easiest way to read an Xml file from C #. I am planning on using an XDocument at the moment, but I was wondering if something was better.

F # makes it so easy to read an Xml file, I wonder if I should translate the language for Xml processing!

+4
source share
4 answers

As already mentioned, C # does not support type providers, and there is no easy way to emulate them (as Gustavo mentioned, you can use the generated type providers through the small F # library, but XML, JSON, CSV and others are not kind.)

I think using the F # library, which is best suited for processing.

A more complex alternative would be to write code that allows you to define types for modeling an XML file in C #, and then automatically reads the file into these classes. I don't think anything like this exists, but I wrote a similar thing in F # (before type providers), so you can see a snippet for inspiration . The usage looks like this:

// Modelling RSS feeds - the following types define the structure of the // expected XML file. The StructuralXml parser can automatically read // XML file into a structure formed by these types. type Title = Title of string type Link = Link of string type Description = Description of string type Item = Item of Title * Link * Description type Channel = Channel of Title * Link * Description * list<Item> type Rss = Rss of Channel // Load data and specify that names in XML are all lowercase let url = "http://feeds.guardian.co.uk/theguardian/world/rss" let doc : StructuralXml<Rss> = StructuralXml.Load(url, LowerCase = true) // Match the data against a type modeling the RSS feed structure let (Rss(channel)) = doc.Root let (Channel(_, _, _, items)) = channel for (Item(Title t, _, _)) in items do printfn "%s" t 
+4
source

Is there a reason why you cannot add the F # library to your solution? This project can create the XML abstraction that you want, and you can use it from your C # projects.

+4
source

No, it is not possible to use type providers directly in C #.

For generated type providers, you can β€œinstantiate” the type provider in an F # project and then use it with C #, but this does not work for erasable type providers such as CsvProvider , JsonProvider , XmlProvider , Freebase or Worldbank . (This will work for the built-in SqlProvider and WsdlProvider, though).

Some discussion was expressed about switching CsvProvider , JsonProvider and XmlProvider to the generated model, so they can be used to bind data in C # ( https://github.com/fsharp/FSharp.Data/issues/134#issuecomment-20104995 ), but this will probably take some time.

My suggestion is either swtich for F # in general, or create record types that reflect the types that the type provider creates. Example:

 type T = { A : int B : string (...) } type XmlT = XmlProvider<"xml file"> let getXml() = let xml = XmlT.GetSample() { A = xml.A B = xml.B (...) } 
+4
source

I took a look at your link to the XmlProvider in F #, and I have not seen anything like it in C #. I use C # with Xml quite a lot and often use XDocument and XElement to load and parse Xml. When I load Xml into an XDocument, I use Linq for Xml to do this. I often use the Linq Pad for general requests in Xml, which works well with C # but not sure about F #.

0
source

Source: https://habr.com/ru/post/1497135/


All Articles