Linq to XML 解析文件夹中的文件
所以我的代码构建没有错误,但我需要改变它打开 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 技术交流群。

发布评论
评论(3)
月棠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; }
}
可爱咩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 电子表格?):当然,因为这是完整的声明性且长的内容, 声明,如果需要的话调试是有点技巧的。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您应该循环遍历
Directory.EnumerateFiles(@"C:\Something", "*.xml")
。You should loop through
Directory.EnumerateFiles(@"C:\Something", "*.xml")
.