我正在根据给定的xml文件创建DataTable。我看过其他资源和类似问题,但仍然停留在同一地方。我想根据我的xml输入文件填充一个表,以便它看起来像这样:
我已经正确插入了DataColumns并没有插入。的行数基于row_no
的数量尝试从bomrow
中的元素中添加值时出现问题。我不确定如何填充这些行,或者我只得到一列在列部分或行部分中分隔。到目前为止,这是我的代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
class Program
{
static IEnumerable<XElement> headerLabels(string xmlFile)
{
using (XmlReader reader = XmlReader.Create(xmlFile))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "bomcol")
{
XElement el = XElement.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
else
reader.Read();
}
}
}
static IEnumerable<XElement> rowValues(string xmlFile)
{
using (XmlReader reader = XmlReader.Create(xmlFile))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "bomcell")
{
XElement el = XElement.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
else
reader.Read();
}
}
}
static IEnumerable<XElement> rowNums(string xmlFile)
{
using (XmlReader reader = XmlReader.Create(xmlFile))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "bomrow")
{
XElement el = XElement.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
}
else
reader.Read();
}
}
}
static void Main(string[] args)
{
DataTable dt = new DataTable();
string xmlFile = @"new.xml";
for (int i = 0; i <= 4; i++)
{
IEnumerable<String> colHeaders =
from el in headerLabels(xmlFile)
where (int)el.Attribute("col_no") == i
select (String)el.Attribute("name");
foreach (String header in colHeaders)
{
dt.Columns.Add(header).ToString();
}
}
Console.WriteLine("Rows: " + dt.Rows.Count);
Console.WriteLine("Cols: " + dt.Columns.Count);
DataColumnCollection cols = dt.Columns;
foreach (DataColumn col in cols)
{
Console.Write(col.ColumnName + "\t");
}
Console.WriteLine();
IEnumerable<String> rows =
from el in rowNums(xmlFile)
where (int)el.Attribute("row_no") >= 0
select (String)el.Attribute("row_no");
foreach (String row_n in rows)
{
DataRow rws = dt.Rows.Add(); //.Add(DATA Values)
}
List<string> rowVals = new List<string>();
foreach (DataRow dtRow in dt.Rows)
{
}
Console.WriteLine("Rows: " + dt.Rows.Count);
Console.WriteLine("Cols: " + dt.Columns.Count);
Console.WriteLine();
Console.ReadLine();
}
}
这是我正在使用的示例xml:
<xml>
<transactions>
<transaction>
<bom>
<bomheader>
<bomcol alignment="center" col_no="0" name="ITEM NO."/>
<bomcol alignment="center" col_no="1" name="ITEMCODE"/>
<bomcol alignment="center" col_no="2" name="PARTNUMBER"/>
<bomcol alignment="center" col_no="3" name="DESCRIPTION"/>
<bomcol alignment="center" col_no="4" name=" QTY."/>
</bomheader>
<bomrow document_id="32" path="\PARTS" row_no="0">
<bomcell col_no="0" value="1"/>
<bomcell col_no="1" value="201"/>
<bomcell col_no="2" value="75"/>
<bomcell col_no="3" value="MEMBER"/>
<bomcell col_no="4" value="2"/>
</bomrow>
<bomrow document_id="35" path="\PARTS" row_no="1">
<bomcell col_no="0" value="2"/>
<bomcell col_no="1" value="205"/>
<bomcell col_no="2" value="75-LH"/>
<bomcell col_no="3" value="MEMBER LEFT HAND"/>
<bomcell col_no="4" value="1"/>
</bomrow>
<bomrow document_id="30" path="\PARTS" row_no="2">
<bomcell col_no="0" value="3"/>
<bomcell col_no="1" value="200"/>
<bomcell col_no="2" value="01AB"/>
<bomcell col_no="3" value="FRAME"/>
<bomcell col_no="4" value="1"/>
</bomrow>
</bom>
</transaction>
</transactions>
</xml>
感谢所有帮助(或资源)!
答案 0 :(得分:0)
您可以像这样用DataSet解决您的问题(这不是一个很好的解决方案,但是可以工作;):
static void Main(string[] args)
{
DataTable dt = new DataTable("Items");
string xmlFile = @"new.xml";
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
foreach (DataRow rowCol in ds.Tables["bomcol"].Rows)
{
dt.Columns.Add(rowCol.ItemArray[2].ToString());
}
DataRow dr = dt.Rows.Add();
for (int j = 0; j < ds.Tables["bomcell"].Rows.Count; j++)
{
var i = j % 5;
if (i == 0 && j != 0)
{
dr = dt.Rows.Add();
}
dr[dt.Columns[i]] = ds.Tables["bomcell"].Rows[j].ItemArray[1];
}
Console.WriteLine("Rows: " + dt.Rows.Count);
Console.WriteLine("Cols: " + dt.Columns.Count);
DataColumnCollection cols = dt.Columns;
foreach (DataColumn col in cols)
{
Console.Write(cols[0] + "\t");
}
foreach (DataRow row in dt.Rows)
{
Console.WriteLine();
Console.Write(row.ItemArray[0] + "\t\t" + row.ItemArray[1] + "\t\t" + row.ItemArray[2] + "\t\t" + row.ItemArray[3] + "\t\t" + row.ItemArray[4] + "\t");
}
Console.ReadLine();
}