开发者

Linq to XML parses files in a folder

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
                 {
     开发者_Python百科                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");
}


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜