Skip to content

Instantly share code, notes, and snippets.

@giacomociti
Created April 23, 2018 20:52
Show Gist options
  • Save giacomociti/5568a8016182e8656cdd45681292dd2b to your computer and use it in GitHub Desktop.
Save giacomociti/5568a8016182e8656cdd45681292dd2b to your computer and use it in GitHub Desktop.
reading a huge xml as a sequence of XElement
#r "System.Xml.Linq"
open System.Xml
open System.Xml.Linq
let elements ns name uri =
seq {
use reader = XmlReader.Create(inputUri = uri)
let onElement() = reader.LocalName = name && reader.NamespaceURI = ns
while onElement() || reader.ReadToFollowing(name, ns) do
yield XNode.ReadFrom(reader) :?> XElement }
let hugeFileUri, numberOfElements = "hugeFileUri.xml", 100
let ns, name = "tempuri.org", "item"
XElement(XName.Get("root", ns),
[| 1..numberOfElements |]
|> Array.map (fun i -> XElement(
XName.Get(name, ns),
XAttribute(XName.Get "id", i))))
.Save(hugeFileUri)
for e in elements ns name hugeFileUri do
printfn "%O" e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment