3 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
嘗試 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貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
根據(jù) jdweng 的回答構(gòu)建:
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."
);
}
這將確保無(wú)論 bomcol 值的輸入(或缺少輸入)如何,表列都將正確設(shè)置為默認(rèn)值。由于 col_no 是唯一重要且可靠的信息,因此這也可以解決列亂序問(wèn)題。

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以像這樣使用 DataSet 解決您的問(wèn)題(這不是一個(gè)漂亮的解決方案,但它有效;):
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 關(guān)注
- 155 瀏覽
添加回答
舉報(bào)