I have an XML file, which I open in F# like this:
我有一个XML文件,我在F#中打开这样的文件:
let Bookmarks(xmlFile:string) =
let xml = XDocument.Load(xmlFile)
Once I have the XDocument I need to navigate it using LINQ to XML and extract all specific tags. Part of my solution is:
一旦我有了XDocument,我需要使用LINQ to XML导航它并提取所有特定的标签。我的部分解决方案是:
let xname (tag:string) = XName.Get(tag)
let tagUrl (tag:XElement) = let attribute = tag.Attribute(xname "href")
attribute.Value
let Bookmarks(xmlFile:string) =
let xml = XDocument.Load(xmlFile)
xml.Elements <| xname "A" |> Seq.map(tagUrl)
How can I extract the specific tags from the XML file?
如何从XML文件中提取特定标记?
2 个解决方案
#1
10
#light
open System
open System.Xml.Linq
let xname s = XName.Get(s)
let bookmarks (xmlFile : string) =
let xd = XDocument.Load xmlFile
xd.Descendants <| xname "bookmark"
This will find all the descendant elements of "bookmark". If you only want direct descendants, use the Elements method (xd.Root.Elements <| xname "whatever").
这将找到“书签”的所有后代元素。如果您只想要直接后代,请使用Elements方法(xd.Root.Elements <| xname“,无论如何”)。
#2
3
Caveat: I've never done linq-to-xml before, but looking through other posts on the topic, this snippet has some F# code that compiles and does something, and thus it may help you get started:
警告:我之前从未做过linq-to-xml,但是通过查看主题的其他帖子,这个代码片段有一些F#代码可以编译并执行某些操作,因此它可以帮助您入门:
open System.IO
open System.Xml
open System.Xml.Linq
let xmlStr = @"<?xml version='1.0' encoding='UTF-8'?>
<doc>
<blah>Blah</blah>
<a href='urn:foo' />
<yadda>
<blah>Blah</blah>
<a href='urn:bar' />
</yadda>
</doc>"
let xns = XNamespace.op_Implicit ""
let a = xns + "a"
let reader = new StringReader(xmlStr)
let xdoc = XDocument.Load(reader)
let aElements = [for x in xdoc.Root.Elements() do
if x.Name = a then
yield x]
let href = xns + "href"
aElements |> List.iter (fun e -> printfn "%A" (e.Attribute(href)))
#1
10
#light
open System
open System.Xml.Linq
let xname s = XName.Get(s)
let bookmarks (xmlFile : string) =
let xd = XDocument.Load xmlFile
xd.Descendants <| xname "bookmark"
This will find all the descendant elements of "bookmark". If you only want direct descendants, use the Elements method (xd.Root.Elements <| xname "whatever").
这将找到“书签”的所有后代元素。如果您只想要直接后代,请使用Elements方法(xd.Root.Elements <| xname“,无论如何”)。
#2
3
Caveat: I've never done linq-to-xml before, but looking through other posts on the topic, this snippet has some F# code that compiles and does something, and thus it may help you get started:
警告:我之前从未做过linq-to-xml,但是通过查看主题的其他帖子,这个代码片段有一些F#代码可以编译并执行某些操作,因此它可以帮助您入门:
open System.IO
open System.Xml
open System.Xml.Linq
let xmlStr = @"<?xml version='1.0' encoding='UTF-8'?>
<doc>
<blah>Blah</blah>
<a href='urn:foo' />
<yadda>
<blah>Blah</blah>
<a href='urn:bar' />
</yadda>
</doc>"
let xns = XNamespace.op_Implicit ""
let a = xns + "a"
let reader = new StringReader(xmlStr)
let xdoc = XDocument.Load(reader)
let aElements = [for x in xdoc.Root.Elements() do
if x.Name = a then
yield x]
let href = xns + "href"
aElements |> List.iter (fun e -> printfn "%A" (e.Attribute(href)))