F# XML 解析 [英] F# XML parsing

查看:16
本文介绍了F# XML 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此 c# 代码可能不是最有效的,但可以完成我想要完成的工作.

this c# code is probably not the most efficient but gets what I want done.

如何在 F# 代码中完成同样的事情?

How do I accomplish the same thing in F# code?

    string xml = " <EmailList> " +
               "      <Email>test@email.com</Email> " +
               "      <Email>test2@email.com</Email> " +
               " </EmailList> ";

    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 ;
        }               
    }

推荐答案

let doc = new XmlDocument() in
    doc.LoadXml xml;
    doc.SelectNodes "/EmailList/Email/text()"
        |> Seq.cast<XmlNode>
        |> Seq.map (fun node -> node.Value)
        |> String.concat Environment.NewLine

如果你真的想要最后的尾随换行符,你可以将它添加到地图和 String.concat 中,并使用空字符串.

If you actually want the final trailing newline you can add it in the map and String.concat with the empty string.

这篇关于F# XML 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆