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

So I have already imported one XML-ish file with 3000 elements and parsed them
I have following XML string: <persons> <person> <name>Someone</name> <age>27</age> </person> <person> <name>Otherone</name> <age>43</age> </person>
I'm an XML newbie and I have an XML document (which I can't edit
I am newbie to jQuery. In netbeans I have jquery-1.7.2.js where all HTML files
Ok so I have these xml files that have date ranges on them. Heres
I'm a bit of a NuGet newbie and have come from the Maven world.
I have a newbie WPF question. Imagine my user control has a namespace declaration
I have a newbie question regarding when to release the elements of a NSArray.
Im newbie here.I have a problem with codeigniter segment() method.I referred 6th segment of
ASP.NET newbie here I have coded up an ASP.NET website and running on win'08

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.