Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 862597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:10:07+00:00 2026-05-15T09:10:07+00:00

I try to tanslate a C# code to F#. But faild. Here is the

  • 0

I try to tanslate a C# code to F#.
But faild.
Here is the C# code from a bolgpost called LINQ Reduces Line Counts and Makes Code “Pop”

It’s about FP.

I translate it to

#r "System.Core.dll"
#r "System.Xml.Linq.dll"

open System
open System.Reflection
open System.Collections
open System.Collections.Generic
open System.Xml.Linq

let (|V|S|A|O|) o =
    if o.GetType().IsValueType then V
    elif o.GetType()=typeof<string> then S
    elif o.GetType().IsArray then A
    else O

let rec obj2xml r o =
    match o with
    | V | S -> new XElement(r, o)
    | A -> o |> Array.map (fun z -> obj2xml r z)
    | O -> new XElement(r, o.GetType().GetProperties() |> Array.map (fun z -> obj2xml (z.Name) (z.GetValue(o, null))))

| V | S -> new XElement(r, o) return a XElement

| A -> o |> Array.map (fun z -> obj2xml r z) return a Array

I couldn’t figure it out!

I haven’t got it complied yet!

Help me please!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:10:08+00:00Added an answer on May 15, 2026 at 9:10 am

    The reason it won’t compile is that all branches of match have to return the same type of object. Your current code returns either an XElement or an Array. Since the name of the function is obj2xml, I guess that the correct type is XElement.

    That means you need to wrap the array in an XElement somehow. I’m going to guess that r is short for ‘root’ and that since all the other XElements returned have r as their first argument, you should pass that.

    | A -> o |> Array.map (fun z -> obj2xml r z)
    // change to:
    | A -> new XElement(r, o |> Array.map (fun z -> obj2xml r z))
    

    By the way, I don’t know if you’re using Visual Studio to write this code. If you are, then you probably want to specify your references via the project system rather than #r compiler directives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i need to play mp3 file from google's texttospeech api at here .But
I have this code. I can receive data from my client successfully, but only
This is an easy question but i am stuck. Here is code for a
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
i just started to learn coffeescript and it's great, but it's tricky.. i try
I have to translate the following code from Java to Scala: EDIT: added if-statements
I need to translate this piece of code from Perl to Lua open(FILE, '/proc/meminfo');
Here is the Java code which I want to translate to C#: public Enumeration
I try to translate a code i use in my templates and js to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.