public FileDataModel ReadXmlFile(string filePath)
{
try
{
if (!File.Exists(filePath))
{
throw new PersistenceValidationException("Validation Error",
new List<BrokenRule>
{
new BrokenRule(BrokenRuleEnum.Data.ToStringValue(), "FileUpload", "File not found.")
});
}
XElement xele = XElement.Load(filePath); //get your file
// declare a new DataTable and pass your XElement to it
DataTable dataTable1 = XElementToDataTable(xele);
// Convert From Table To Object
var fileDataModel = ConvertFromTableToObject(dataTable1);
return fileDataModel;
}
catch (XmlException)
{
throw new PersistenceValidationException("Validation Error", new List<BrokenRule>
{
new BrokenRule("InvalidXml", "Invalid Xml file")
});
}
}
------------------
public DataTable XElementToDataTable(XElement x)
{
DataTable dtable = new DataTable();
XElement setup = (from p in x.Descendants() select p).First();
// build your DataTable
foreach (XElement xe in setup.Descendants())
dtable.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt
var all = from p in x.Descendants(setup.Name.ToString()) select p;
foreach (XElement xe in all)
{
DataRow dr = dtable.NewRow();
foreach (XElement xe2 in xe.Descendants())
dr[xe2.Name.ToString()] = xe2.Value; //add in the values
dtable.Rows.Add(dr);
}
return dtable;
}
------------------
private static FileDataModel ConvertFromTableToObject(DataTable dataTable1)
{
var fileDataModel = new FileDataModel();
var listDataResult = new List<List<FieldImports>>();
var columnList = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnName = Convert.ToString(dataTable1.Columns[columnIndex]);
columnList.Add(result);
}
for (var rowIndex = 0; rowIndex < dataTable1.Rows.Count; rowIndex++)
{
var fileData = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnValue = Convert.ToString(dataTable1.Rows[rowIndex][columnIndex]);
fileData.Add(result);
}
listDataResult.Add(fileData);
}
fileDataModel.fileData = listDataResult;
fileDataModel.columnList = columnList;
return fileDataModel;
}
{
try
{
if (!File.Exists(filePath))
{
throw new PersistenceValidationException("Validation Error",
new List<BrokenRule>
{
new BrokenRule(BrokenRuleEnum.Data.ToStringValue(), "FileUpload", "File not found.")
});
}
XElement xele = XElement.Load(filePath); //get your file
// declare a new DataTable and pass your XElement to it
DataTable dataTable1 = XElementToDataTable(xele);
// Convert From Table To Object
var fileDataModel = ConvertFromTableToObject(dataTable1);
return fileDataModel;
}
catch (XmlException)
{
throw new PersistenceValidationException("Validation Error", new List<BrokenRule>
{
new BrokenRule("InvalidXml", "Invalid Xml file")
});
}
}
------------------
public DataTable XElementToDataTable(XElement x)
{
DataTable dtable = new DataTable();
XElement setup = (from p in x.Descendants() select p).First();
// build your DataTable
foreach (XElement xe in setup.Descendants())
dtable.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt
var all = from p in x.Descendants(setup.Name.ToString()) select p;
foreach (XElement xe in all)
{
DataRow dr = dtable.NewRow();
foreach (XElement xe2 in xe.Descendants())
dr[xe2.Name.ToString()] = xe2.Value; //add in the values
dtable.Rows.Add(dr);
}
return dtable;
}
------------------
private static FileDataModel ConvertFromTableToObject(DataTable dataTable1)
{
var fileDataModel = new FileDataModel();
var listDataResult = new List<List<FieldImports>>();
var columnList = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnName = Convert.ToString(dataTable1.Columns[columnIndex]);
columnList.Add(result);
}
for (var rowIndex = 0; rowIndex < dataTable1.Rows.Count; rowIndex++)
{
var fileData = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnValue = Convert.ToString(dataTable1.Rows[rowIndex][columnIndex]);
fileData.Add(result);
}
listDataResult.Add(fileData);
}
fileDataModel.fileData = listDataResult;
fileDataModel.columnList = columnList;
return fileDataModel;
}
No comments:
Post a Comment