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 622789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:01:20+00:00 2026-05-13T19:01:20+00:00

F# newbie I have 2 XML files in 2 folders c:\root\a\file.xml and c:\root\b\file.xml They

  • 0

F# newbie

I have 2 XML files in 2 folders c:\root\a\file.xml and c:\root\b\file.xml

They have identical structure:

<parent>
   <property name="firstName">Jane</property>
   <property name="lastName">...</property>
   <property name="dateOfBirth">...</property>>
</parent>

I need to chose the file which property node with name firstName has the value Jane.

In F# (possibly by using System.Xml.Linq)
I have tried several solutions but none working so far. Anyone willing to help?

  • 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-13T19:01:21+00:00Added an answer on May 13, 2026 at 7:01 pm

    It would be useful if you could show some code that you tried – someone can then explain what is the problem, so you can learn more than when someone just posts code that works. Anyway, you’ll need to reference some assemblies with the System.Xml.Linq and open the namespace first. In F# interactive, you can write it like this (in F# project, just use Add Reference dialog):

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

    When using XLinq in F#, you need a simple utility function for converting strings to XName object (which represents an element/attribute name). There is an implicit conversion in C#, but this sadly doesn’t work in F#.

    let xn s = XName.Get(s)
    

    Then you can load your XML document using the XDocument class and use Element method to get a single “parent” element. Then you can call Elements to get all nested “property” elements:

    let xd = XDocument.Load("file.xml")
    let props = xd.Element(xn "parent").Elements(xn "property")
    

    Now you can search the elements to find the one element with the specified attribute value. For example using Seq.tryFind (which also allows you to handle the case when the element is not found):

    let nameOpt = props |> Seq.tryFind (fun xe -> 
      xe.Attribute(xn "name").Value = "firstName")
    

    Now, the value nameOpt is of type option<XElement> so you can pattern match on it to see if the element was found (e.g. Some(el)) or if it wasn’t found (None).

    EDIT: Another way to write this is to use sequence expressions and then just take the first element (this doesn’t handle the case when element is not found):

    let nameEl = 
      seq { for el in xd.Element(xn "parent").Elements(xn "property") do
              if xe.Attribute(xn "name").Value = "firstName" then yield xe }
      |> Seq.head
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Warning - asp newbie) I have an aspx file with the tag <%@ Page
Ok so I have these xml files that have date ranges on them. Heres
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
Java Newbie here. I have a JFrame that I added to my netbeans project,
I am a newbie to WPF and have a couple of questions about WPF
I am a newbie to Python and have come across the following example in
I'm a ClearCase newbie and up until now have been used to SVN. Therefore,
I'm a newbie to pgsql. I have few questionss on it: 1) I know
I'm a newbie to swing development. I have a swing app that needs to
As being quite a newbie in linux, I have the follwing question. I have

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.