3 回答
TA贡献1784条经验 获得超7个赞
尝试 xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ITEM NO.", typeof(int));
dt.Columns.Add("ITEMCODE", typeof(string));
dt.Columns.Add("PARTNUMBER.", typeof(string));
dt.Columns.Add("DESCRIPTION", typeof(string));
dt.Columns.Add("QTY.", typeof(int));
XDocument doc = XDocument.Load(FILENAME);
foreach (XElement bomrow in doc.Descendants("bomrow"))
{
dt.Rows.Add(new object[] {
bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 0).FirstOrDefault() == null ?
null : (int?)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 0).FirstOrDefault().Attribute("value"),
bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 1).FirstOrDefault() == null ?
null : (string)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 1).FirstOrDefault().Attribute("value"),
bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 2).FirstOrDefault() == null ?
null : (string)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 2).FirstOrDefault().Attribute("value"),
bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 3).FirstOrDefault() == null ?
null : (string)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 3).FirstOrDefault().Attribute("value"),
bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 4).FirstOrDefault() == null ?
null : (int?)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 4).FirstOrDefault().Attribute("value")
});
}
}
}
}
TA贡献1846条经验 获得超7个赞
根据 jdweng 的回答构建:
foreach(XElement bomheader in doc.Descendants("bomheader"))
{
dt.Columns.Add(
bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 0).FirstOrDefault() == null ?
null : "ITEM NO."
);
dt.Columns.Add(
bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 1).FirstOrDefault() == null ?
null : "ITEMCODE"
);
dt.Columns.Add(
bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 2).FirstOrDefault() == null ?
null : "PARTNUMBER"
);
dt.Columns.Add(
bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 3).FirstOrDefault() == null ?
null : "DESCRIPTION"
);
dt.Columns.Add(
bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 4).FirstOrDefault() == null ?
null : "QTY."
);
}
这将确保无论 bomcol 值的输入(或缺少输入)如何,表列都将正确设置为默认值。由于 col_no 是唯一重要且可靠的信息,因此这也可以解决列乱序问题。
TA贡献1876条经验 获得超7个赞
您可以像这样使用 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();
}
- 3 回答
- 0 关注
- 131 浏览
添加回答
举报