F#XML解析 [英] F# XML parsing
本文介绍了F#XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个c#代码可能不是最高效的,但得到了我想要做的。
我如何在F#代码中完成同样的事情?
< 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(nodeList中的XmlNode项目)
{
foreach(XmlNode电子邮件中的项目)
{
emailList + = email.InnerText.ToString()+ Environment.NewLine ;
$ / code $ / pre
解决方案 <$ p $在
doc.LoadXml xml中让doc = new XmlDocument()
doc.SelectNodes/ EmailList / Email / text()
|> Seq.cast< XmlNode的>
|> Seq.map(fun节点 - > node.Value)
|> String.concat Environment.NewLine
如果您真的想要最后的换行符,您可以将其添加到映射中和String.concat中的空字符串。
this c# code is probably not the most efficient but gets what I want done.
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
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屋!
查看全文