猿问

XML 填充数据表

我正在从给定的 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();

            }

        }

    }

    

梵蒂冈之花
浏览 132回答 3
3回答

噜噜哒

尝试 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{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("ITEM NO.", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("ITEMCODE", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("PARTNUMBER.", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("DESCRIPTION", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("QTY.", typeof(int));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (XElement bomrow in doc.Descendants("bomrow"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Rows.Add(new object[] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 0).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : (int?)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 0).FirstOrDefault().Attribute("value"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 1).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : (string)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 1).FirstOrDefault().Attribute("value"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 2).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : (string)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 2).FirstOrDefault().Attribute("value"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 3).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : (string)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 3).FirstOrDefault().Attribute("value"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 4).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null : (int?)bomrow.Elements("bomcell").Where(x => (int)x.Attribute("col_no") == 4).FirstOrDefault().Attribute("value")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

喵喵时光机

根据 jdweng 的回答构建:foreach(XElement bomheader in doc.Descendants("bomheader"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 0).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null : "ITEM NO."&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 1).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null : "ITEMCODE"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 2).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : "PARTNUMBER"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 3).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : "DESCRIPTION"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bomheader.Elements("bomcol").Where(x => (int)x.Attribute("col_no") == 4).FirstOrDefault() == null ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null : "QTY."&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }这将确保无论 bomcol 值的输入(或缺少输入)如何,表列都将正确设置为默认值。由于 col_no 是唯一重要且可靠的信息,因此这也可以解决列乱序问题。

幕布斯6054654

您可以像这样使用 DataSet 解决您的问题(这不是一个漂亮的解决方案,但它有效;):&nbsp;static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable("Items");&nbsp; &nbsp; &nbsp; &nbsp; string xmlFile = @"new.xml";&nbsp; &nbsp; &nbsp; &nbsp; DataSet ds = new DataSet();&nbsp; &nbsp; &nbsp; &nbsp; ds.ReadXml(xmlFile);&nbsp; &nbsp; &nbsp; &nbsp; foreach (DataRow rowCol in ds.Tables["bomcol"].Rows)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add(rowCol.ItemArray[2].ToString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; DataRow dr = dt.Rows.Add();&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < ds.Tables["bomcell"].Rows.Count; j++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i = j % 5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == 0 && j != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr = dt.Rows.Add();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dr[dt.Columns[i]] = ds.Tables["bomcell"].Rows[j].ItemArray[1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Rows: " + dt.Rows.Count);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Cols: " + dt.Columns.Count);&nbsp; &nbsp; &nbsp; &nbsp; DataColumnCollection cols = dt.Columns;&nbsp; &nbsp; &nbsp; &nbsp; foreach (DataColumn col in cols)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write(cols[0] + "\t");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach (DataRow row in dt.Rows)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答