这个c#代码可能不是最有效的,但得到我想要的.
如何在F#代码中完成同样的事情?
string xml = "" + " "; XmlDocument xdoc = new XmlDocument(); XmlNodeList nodeList; String emailList = string.Empty; xdoc.LoadXml(xml); nodeList = xdoc.SelectNodes("//EmailList"); foreach (XmlNode item in nodeList) { foreach (XmlNode email in item) { emailList += email.InnerText.ToString() + Environment.NewLine ; } }test@email.com " + "test2@email.com " + "
Derek Slager.. 48
let doc = new XmlDocument() in doc.LoadXml xml; doc.SelectNodes "/EmailList/Email/text()" |> Seq.cast|> Seq.map (fun node -> node.Value) |> String.concat Environment.NewLine
如果你真的想要最终的尾随换行符,可以在地图中添加它,并使用空字符串添加String.concat.
let doc = new XmlDocument() in doc.LoadXml xml; doc.SelectNodes "/EmailList/Email/text()" |> Seq.cast|> Seq.map (fun node -> node.Value) |> String.concat Environment.NewLine
如果你真的想要最终的尾随换行符,可以在地图中添加它,并使用空字符串添加String.concat.
与F#Data XML Type Provider一样:
type EmailList = XmlProvider<""""""> let data = EmailList.Parse(""" """) let emailList = data.Emails |> String.concat Environment.NewLine test@email.com test2@email.com
这是在F#中执行此操作的类似代码.我相信其中一个F#忍者会在一分钟之内将更好的版本放在这里.
open System.Xml let getList x = let getDoc = let doc = new XmlDocument() doc.LoadXml(x) |> ignore doc let getEmail (n:XmlNode) = n.InnerText.ToString() let doc = getDoc let build = new System.Text.StringBuilder() doc.SelectNodes("//EmailList") |> Seq.cast|> Seq.map (fun n -> n.ChildNodes ) |> Seq.map_concat (Seq.cast ) |> Seq.map(fun (n:XmlNode) -> getEmail n) |> Seq.iter (fun e -> build.AppendLine(e) |> ignore ) build.ToString()