【问题标题】:LINQ algorithm to create generic table from XMLLINQ 算法从 XML 创建通用表
【发布时间】:2023-03-14 00:08:02
【问题描述】:

这是一段代码:

XNamespace z = "#SomeSchema";
var listCols = new HashSet<Col>();
var colNameList = new List<string>(..some values..);

var xElementList = doc.Descendants(z + "row");

return new HashSet<Row>(xElementList .Select(x=> new Row
       {
         Col= new List<Col>(listCols).Select(col =>
         {
           col.Value= (string)x.Attribute(colNameList.First(colName=> colName == col.Name));
           return col;
         }).ToList()
       }));

错误在于,返回值包含一个行列表,但所有这些行都具有完全相同的值(对于 Col 值)。

例如,行[1].Col[1].Value == Row[2].Col[2].Value

而且这些值应该完全不同。我正在从 Xml 文件中获取这些值。当我调试 xElementList 时,值是不同的,但是当我尝试使用它们创建行时,所有行都是相同的。 实际上,Rows 有相同的 Columns 列表,这是 xElementList 的最后一条记录。

我做错了吗?

谢谢。

【问题讨论】:

    标签: c# xml linq xelement


    【解决方案1】:

    见下面的代码。我读了两次xml。一次获取列名并将列添加到表中。然后第二次读取xml获取行数据。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
    
                StreamReader sReader = new StreamReader(FILENAME, Encoding.GetEncoding(1252));
    
                XmlReader reader = XmlReader.Create(sReader);
                Dictionary<string, string> colDict = new Dictionary<string, string>();
                while (!reader.EOF)
                {
                    if (reader.Name != "FIELD")
                    {
                        reader.ReadToFollowing("FIELD");
                    }
                    if (!reader.EOF)
                    {
                        XElement field = (XElement)XElement.ReadFrom(reader);
                        string attrname = (string)field.Attribute("attrname");
                        string fieldtype = (string)field.Attribute("fieldtype");
                        switch (fieldtype)
                        {
                            case "string":
                                dt.Columns.Add(attrname, typeof(string));
                                break;
                            case "i4":
                                dt.Columns.Add(attrname, typeof(int));
                                break;
                        }
                        colDict.Add(attrname, fieldtype);
                    }
                }
                reader.Close();
                sReader = new StreamReader(FILENAME, Encoding.GetEncoding(1252));
                reader = XmlReader.Create(sReader);
                while (!reader.EOF)
                {
                    if (reader.Name != "ROW")
                    {
                        reader.ReadToFollowing("ROW");
                    }
                    if (!reader.EOF)
                    {
                        XElement row = (XElement)XElement.ReadFrom(reader);
                        DataRow newRow = dt.Rows.Add();
                        foreach (XAttribute attrib in row.Attributes())
                        {
                            string colName = attrib.Name.LocalName;
                            if (colDict.ContainsKey(colName))
                            {
                                switch (colDict[colName])
                                {
                                    case "string":
                                        newRow[colName] = (string)attrib;
                                        break;
                                    case "i4":
                                        newRow[colName] = (int)attrib;
                                        break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多