Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use LINQ to traverse a specified directory and output to XML

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use LINQ to traverse a specified directory and output it to XML". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem: use LINQ to traverse a specified directory and output to XML.

The code is as follows:

Public static XElement ToXml (this DirectoryInfo dir) {XElement root = new XElement ("Dir", new XAttribute ("Name", dir.Name), from subDir in dir.GetDirectories () select DirToXml (subDir), from file in dir.GetFiles () select new XElement ("File", file.Name); return root;}

When using it, you can do this:

DirectoryInfo dir = new DirectoryInfo (path); XElement dirdirXml = dir.ToXml (); OK, done. As you can see, LINQ is very powerful. Since it is so powerful, why not go further? What should I do if I want to traverse the formatted files in the directory? For example, I want to find out all the mp3 in the directory, so I can write code like this.

Public static XElement ToXml (this DirectoryInfo dir, string searchPattern) {XElement root = new XElement ("Dir", new XAttribute ("Name", dir.Name), from subDir in dir.GetDirectories () select DirToXml (subDir), from file in dir.GetFiles (searchPattern) select new XElement ("File", file.Name)); return root;} / / DirectoryInfo dir = new DirectoryInfo (path); XElement dirdirXml = dir.DirToXml ("* .mp3")

It's easy to take it a step further. What if I'm looking for files in multiple formats? For example, to find out the picture files in the directory (jpg, bmp, gif … (Ok, no problem, it can be written like this:

Public static XElement ToXml (this DirectoryInfo dir, string [] searchPatterns) {XElement root = new XElement ("Dir", new XAttribute ("Name", dir.Name), from subDir in dir.GetDirectories () select DirToXml (subDir), from pattern in searchPattern from file in dir.GetFiles (pattern) select new XElement ("File", file.Name); return root;} can be used like this: DirectoryInfo dir = new DirectoryInfo (path) XElement dirdirXml = dir.DirToXml (new [] {"* .mp3", "* .txt"}); "how to use LINQ to traverse a specified directory and output it to XML" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report