Write xml file output values to a dataset
This code is programmed to display some data values from alot of xml files is their anyway to alter it so that it write the values to a dataset/table?
static void Main(string[] args)
{
string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
foreach (string fileName in fileEntries)
{
XDocument doc = XDocument.Load(fileName);
var query = from x in doc.Descendants("XAxisCalib")
select new
{
//Max1 = x.Attribute("Max").Value,
//Min2 = x.Attribute("Min").Value
MaxChild = x.Descendants("Max"),
MinChild = x.Descendants("Min")
};
foreach (var x in query)
{
foreach (var nextLevel in x.MaxChild)
{
Console.WriteLine("XMax: " + nextLevel.Value);
}
foreach (var nextLevel in x.MinChild)
{
Console.WriteLine("XMin: " + nextLevel.Value);
}
//Console.WriteLine("XAxisCalib");
}
var query2 = from y in doc.Descendants("YAxisCalib")
select new
{
开发者_如何学JAVA //Max3 = x.Attribute("Max").Value,
//Min4 = x.Attribute("Min").Value
MaxChild = y.Descendants("Max"),
MinChild = y.Descendants("Min")
};
foreach (var y in query2)
{
foreach (var nextLevel in y.MaxChild)
{
Console.WriteLine("YMax: " + nextLevel.Value);
}
foreach (var nextLevel in y.MinChild)
{
Console.WriteLine("YMin: " + nextLevel.Value);
}
//Console.WriteLine("YAxisCalib");
var query3 = from z in doc.Descendants("ZAxisCalib")
select new
{
//Max5 = x.Attribute("Max").Value,
//Min6 = x.Attribute("Min").Value
MaxChild = z.Descendants("Max"),
MinChild = z.Descendants("Min")
};
foreach (var z in query3)
{
foreach (var nextLevel in z.MaxChild)
{
Console.WriteLine("ZMax: " + nextLevel.Value);
}
foreach (var nextLevel in z.MinChild)
{
Console.WriteLine("ZMin: " + nextLevel.Value);
}
//Console.WriteLine("ZAxisCalib");
}
}
}
}
}
}
I don't know if I'm missing something, but what about the DataSet.ReadXml method?:
DataSet ds = new DataSet();
ds.ReadXml("myxmlfile.xml");
The ReadXml()
method has an overload for passing in XmlReadMode
, which provides various options handling the schema.
In your case, assuming that you want to read each XML file into it's own DataSet, you can do something like this:
string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
foreach (string fileName in fileEntries)
{
DataSet ds = new DataSet();
ds.ReadXml(fileName, XmlReadMode.InferSchema);
}
To read the XML files into the same DataSet, you can do something like this:
DataSet masterSet = new DataSet();
string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
foreach (string fileName in fileEntries)
{
//initialize a new dataset and read the xml into it
DataSet tempSet = new DataSet();
tempSet.ReadXml(fileName, XmlReadMode.InferSchema);
//merge the tables from the temporary datset into the master dataset
foreach (DataTable table in tempSet.Tables)
masterSet.Merge(table);
}
Here's another way of doing the same thing, using the enumerable LINQ methods:
DataSet masterSet = new DataSet();
string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
foreach (string fileName in fileEntries)
{
//initialize a new dataset and read the xml into it
DataSet tempSet = new DataSet();
tempSet.ReadXml(fileName, XmlReadMode.InferSchema);
//merge the tables from the temporary datset into the master dataset
tempSet.Tables.Cast<DataTable>().ToList().ForEach(table => masterSet.Merge(table));
}
One of the XmlReadMode
enumerations should definitely suit your needs.
- Auto
- DiffGram
- Fragment
- IgnoreSchema
- InferSchema
- InferTypedSchema
- ReadSchema
Here is a link on MSDN that explains what the different XmlReadMode enumerations do:
http://msdn.microsoft.com/en-us/library/system.data.xmlreadmode.aspx
精彩评论