Linq to XML 解析文件夹中的文件

发布于 12-03 17:12 字数 1075 浏览 3 评论 0原文

所以我的代码构建没有错误,但我需要改变它打开 xml 文档的方式。现在它可以打开一个 xml 文档,我需要它做的是在我的 c: 上打开一个文件夹,并解析该文件夹中的所有 xml 文件。有什么帮助吗?

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\.cfg");  //Change here
    var query = from x in doc.Descendants("X")
                select new
                {
                    Max1 = x.Attribute("Max").Value,
                    Min2 = x.Attribute("Min").Value
                };
    foreach (var x in query) ;
    Console.WriteLine("X");
    var query2 = from x in doc.Descendants("Y")
                 select new
                 {
                     Max3 = x.Attribute("Max").Value,
                     Min4 = x.Attribute("Min").Value
                 };
    foreach (var x in query2)
        Console.WriteLine("Y");
    var query3 = from x in doc.Descendants("ZA")
                 select new
                 {
                     Max5 = x.Attribute("Max").Value,
                     Min6 = x.Attribute("Min").Value
                 };
    foreach (var x in query3)
        Console.WriteLine("Z");
}

So I have this code building with no errors but I need to alter how its opening the xml documents. Right now it can open a single xml documents what I need it to do is open up a folder on my c: and parse through all the xml files in the folder. Any help?

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\.cfg");  //Change here
    var query = from x in doc.Descendants("X")
                select new
                {
                    Max1 = x.Attribute("Max").Value,
                    Min2 = x.Attribute("Min").Value
                };
    foreach (var x in query) ;
    Console.WriteLine("X");
    var query2 = from x in doc.Descendants("Y")
                 select new
                 {
                     Max3 = x.Attribute("Max").Value,
                     Min4 = x.Attribute("Min").Value
                 };
    foreach (var x in query2)
        Console.WriteLine("Y");
    var query3 = from x in doc.Descendants("ZA")
                 select new
                 {
                     Max5 = x.Attribute("Max").Value,
                     Min6 = x.Attribute("Min").Value
                 };
    foreach (var x in query3)
        Console.WriteLine("Z");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔2024-12-10 17:12:18

您应该循环遍历Directory.EnumerateFiles(@"C:\Something", "*.xml")

You should loop through Directory.EnumerateFiles(@"C:\Something", "*.xml").

月棠2024-12-10 17:12:18

...稍微更“声明性”的方式:

// Program.cs
class Program
{
    static void Main(string[] args)
    {
        const string path = @"C:\stuff";
        Parallel.ForEach(Directory.EnumerateFiles(path, "*.xml"), x => Walk(XDocument.Load(x)));
    }

    static IEnumerable<Calib> MapItem(IEnumerable<XElement> elements)
    {
        return elements.Select(x => new Calib
        {
            Max = x.Attribute("Max").Value,
            Min = x.Attribute("Min").Value
        });
    }

    static void Walk(XDocument doc)
    {
        var xitems = MapItem(doc.Descendants("XaxisCalib"));
        xitems.Iter(x => Console.WriteLine("(XaxisCalib) X: Min = {0} | Max = {1}", x.Min, x.Max));

        var yitems = MapItem(doc.Descendants("YAxisCalib"));
        yitems.Iter(x => Console.WriteLine("(YaxisCalib) Y: Min = {0} | Max = {1}", x.Min, x.Max));

        var zitems = MapItem(doc.Descendants("ZAxisCalib"));
        zitems.Iter(x => Console.WriteLine("(ZaxisCalib) Z: Min = {0} | Max = {1}", x.Min, x.Max));
    }
}

// Exts.cs
public static class Exts
{
    public static void Iter<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var item in source)
        {
            action(item);
        }
    }
}

// Calib.cs
public class Calib
{
    public string Max { get; set; }
    public string Min { get; set; }
}

... A slightly more "declarative" manner:

// Program.cs
class Program
{
    static void Main(string[] args)
    {
        const string path = @"C:\stuff";
        Parallel.ForEach(Directory.EnumerateFiles(path, "*.xml"), x => Walk(XDocument.Load(x)));
    }

    static IEnumerable<Calib> MapItem(IEnumerable<XElement> elements)
    {
        return elements.Select(x => new Calib
        {
            Max = x.Attribute("Max").Value,
            Min = x.Attribute("Min").Value
        });
    }

    static void Walk(XDocument doc)
    {
        var xitems = MapItem(doc.Descendants("XaxisCalib"));
        xitems.Iter(x => Console.WriteLine("(XaxisCalib) X: Min = {0} | Max = {1}", x.Min, x.Max));

        var yitems = MapItem(doc.Descendants("YAxisCalib"));
        yitems.Iter(x => Console.WriteLine("(YaxisCalib) Y: Min = {0} | Max = {1}", x.Min, x.Max));

        var zitems = MapItem(doc.Descendants("ZAxisCalib"));
        zitems.Iter(x => Console.WriteLine("(ZaxisCalib) Z: Min = {0} | Max = {1}", x.Min, x.Max));
    }
}

// Exts.cs
public static class Exts
{
    public static void Iter<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var item in source)
        {
            action(item);
        }
    }
}

// Calib.cs
public class Calib
{
    public string Max { get; set; }
    public string Min { get; set; }
}
可爱咩2024-12-10 17:12:18

而不是仅仅将值写入控制台

var fileData = new XElement("root",
 from file in New System.IO.DirectoryInfo("C:\Something").GetFiles() 
    where file.Extension.Equals(".xml", String Comparison.CurrentCultureIgnoreCase) 
    Let doc = XElement.Load(file.FullName)
    select new XElement("File", 
      new XAttribute("Path", file.FullName), 
      select new XElement("XAxisCalibs", 
         from x in doc.Descendants("XAxisCalib") 
         select new XElement("XAxisCalib", 
              new XAttribute("Max", x.Attribute("Max").Value),
              new XAttribute("Min", x.Attribute("Min").Value)
         )
      ),
      select new XElement("YAxisCalibs", 
         from y in doc.Descendants("YAxisCalib") 
         select new XElement("YAxisCalib", 
              new XAttribute("Max", x.Attribute("Max").Value),
              new XAttribute("Min", x.Attribute("Min").Value)
         )
      ),
      select new XElement("ZAxisCalibs", 
         from z in doc.Descendants("ZAxisCalib") 
         select new XElement("ZAxisCalib", 
              new XAttribute("Max", x.Attribute("Max").Value),
              new XAttribute("Min", x.Attribute("Min").Value)
         )
      )
    );

您可以根据文件中的值创建一个新的 Xml 文档,然后执行您想要的任何操作(生成 Excel 电子表格?):当然,因为这是完整的声明性且长的内容, 声明,如果需要的话调试是有点技巧的。

Rather than just writing the values out to the console, you could create a new Xml document from the values in the files and do whatever you want with from that (generate an Excel spreadsheet?):

var fileData = new XElement("root",
 from file in New System.IO.DirectoryInfo("C:\Something").GetFiles() 
    where file.Extension.Equals(".xml", String Comparison.CurrentCultureIgnoreCase) 
    Let doc = XElement.Load(file.FullName)
    select new XElement("File", 
      new XAttribute("Path", file.FullName), 
      select new XElement("XAxisCalibs", 
         from x in doc.Descendants("XAxisCalib") 
         select new XElement("XAxisCalib", 
              new XAttribute("Max", x.Attribute("Max").Value),
              new XAttribute("Min", x.Attribute("Min").Value)
         )
      ),
      select new XElement("YAxisCalibs", 
         from y in doc.Descendants("YAxisCalib") 
         select new XElement("YAxisCalib", 
              new XAttribute("Max", x.Attribute("Max").Value),
              new XAttribute("Min", x.Attribute("Min").Value)
         )
      ),
      select new XElement("ZAxisCalibs", 
         from z in doc.Descendants("ZAxisCalib") 
         select new XElement("ZAxisCalib", 
              new XAttribute("Max", x.Attribute("Max").Value),
              new XAttribute("Min", x.Attribute("Min").Value)
         )
      )
    );

Granted, since this is complete declarative and one long statement, it is a bit of a trick to debug if necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文