我正在尝试将XML文件转换为CSV,但是我正在运行的.NET版本不支持似乎在这里流行的CsvHelper库。
以下简化了XML文档:
<E1AFKOL SEGMENT="1">
<MATNR>456654546</MATNR>
<E1AFFLL SEGMENT="1">
<APLZL>00000001</APLZL>
<E1AFVOL SEGMENT="1">
<VORNR>0020</VORNR>
<E1RESBL SEGMENT="1">
<AUSCH>0.00</AUSCH>
</E1RESBL>
<E1RESBL SEGMENT="1">
<AUSCH>0.00</AUSCH>
<bdaRT>AR</bdaRT>
<BDMNG>1.000</BDMNG>
<BDTER>20190626</BDTER>
<MATNR>LD1000345</MATNR>
<MEINS>EA</MEINS>
<VMENG>1.000</VMENG>
<WERKS>1110</WERKS>
</E1RESBL>
</E1AFVOL>
</E1AFFLL>
</E1AFKOL>
在第0列中,我只需要一个 MATNR 标记(主键)。 E1RESBL 标记的内容是需要添加的值(第1-6列)但并不需要结构中的所有标签。
我将XML文档作为XElement和列名列表传递。它在一个列表中返回所有值。
public static List<string> makeRows(XElement xmlD,List<string> columnNames) {
List<string> columnNames = new List<string>();
string[] wkOrdColumnNames = { "AUFNR","AUSCH","BDMNG","BDTER","MATNR","MEINS","WERKS" }; // all other fields needed
columnNames.AddRange(wkOrdColumnNames); // adds the others
// Load each structure
var E1RESBL = xmlD.XPathSelectElements(@"/IDOC/E1AFKOL/E1AFFLL/E1AFVOL/E1RESBL"); // XML structure within doc
var E1AFKOL = xmlD.XPathSelectElements(@"/IDOC/E1AFKOL"); // column0 structure
List<string> tagValues = new List<string>(); // all values from the tags are stored here
// Where the WorkOrderID is added to the the values
foreach (var innerNode in E1AFKOL) { // loops through the parent structure
foreach (var innerElement in innerNode.XPathSelectElements(columnNames[0])) // passes the element(AUFNR),gets the value of that tag
tagValues.Add(innerElement.Value); // adds it to the List
}
// The rest of the fields are added here
foreach (var innerNode in E1RESBL) { // loops through all tags in E1RESBL structure
foreach (string i in columnNames) { // loops through each string in the array of column names
foreach (var innerElement in innerNode.XPathSelectElements(i))
tagValues.Add(innerElement.Value);
}
}
return tagValues;
}
然后我将列名和值传递给DataTable。
public static DataTable makeDataTable(List<string> columns,List<string> rows) {
// Make Data table
DataTable dataT = new DataTable();
DataColumn dCol = new DataColumn();
// Add columns
foreach (var i in columns) {
dCol.DataType = Type.GetType("System.String");
dCol.ColumnName = i;
dataT.Columns.Add(dCol.ColumnName,dCol.DataType);
}
// Add rows
dataT.Rows.Add(rows[0]); // adds to column 0
for (int r = 1; r < (rows.Capacity-1); r++) { // starts after the 0th element (wkOrdID)
DataRow dr = dataT.NewRow(); // Adds a new row
Console.WriteLine(rows[r]);
for (int j = 1; j < 7; j++) { // skips the 0th column
dr[j] = rows[r]; // adds the values of the list to the row
}
dataT.Rows.Add(dr);
}
我知道可能有一种更简单的方法来执行此操作,因此,如果您可以帮助的话,将不胜感激。