Skip to main content
 首页 » 编程设计

c#之组合或连接从单独的 XML 文件中读取的对象的最有效方法

2024年02月20日11itcoder

我有大量数据,每天早上都以单独的 XML 文件形式接收。我需要组合 XML 中的对象并从中生成报告。我正在寻找针对此问题的最佳解决方案。

为了证明我编造了以下示例:

有 2 个 XML 文件:

第一个是语言列表及其使用的国家/地区。第二个是产品列表及其销售国家/地区。我生成的报告是产品名称,后跟包装必须使用的语言中。

XML1:

<?xml version="1.0" encoding="utf-8"?> 
<languages> 
  <language> 
    <name>English</name> 
    <country>8</country> 
    <country>9</country> 
    <country>3</country> 
    <country>11</country> 
    <country>12</country> 
  </language> 
  <language> 
    <name>French</name> 
    <country>3</country> 
    <country>6</country> 
    <country>7</country> 
    <country>13</country> 
  </language> 
  <language> 
    <name>Spanish</name> 
    <country>1</country> 
    <country>2</country> 
    <country>3</country> 
  </language> 
</languages> 

XML2:

<?xml version="1.0" encoding="utf-8"?> 
<products> 
  <product> 
    <name>Screws</name> 
    <country>3</country> 
    <country>12</country> 
    <country>29</country> 
  </product> 
  <product> 
    <name>Hammers</name> 
    <country>1</country> 
    <country>13</country> 
  </product> 
  <product> 
    <name>Ladders</name> 
    <country>12</country> 
    <country>39</country> 
    <country>56</country> 
  </product> 
  <product> 
    <name>Wrenches</name> 
    <country>8</country> 
    <country>13</country> 
    <country>456</country> 
  </product> 
  <product> 
    <name>Levels</name> 
    <country>19</country> 
    <country>18</country> 
    <country>17</country> 
  </product> 
</products> 

示例程序输出:

 Screws ->  English, French, Spanish 
 Wrenches ->  English, French 
 Hammer - > French, Spanish 
 Ladders-> English 

目前我反序列化为数据集,然后使用 linq 连接数据集以生成所需的报告字符串。 (如下所示 - 将文件名作为命令行参数传递)。

public static List<String> XMLCombine(String[] args) 
{ 
    var output = new List<String>(); 
    var dataSets = new List<DataSet>(); 
    //Load each of the Documents specified in the args 
    foreach (var s in args) 
    { 
        var path = Environment.CurrentDirectory + "\\" + s; 
        var tempDS = new DataSet(); 
        try 
        { 
            tempDS.ReadXml(path); 
        } 
        catch (Exception ex) 
        { 
            //Custom Logging + Error Reporting 
            return null; 
        } 
        dataSets.Add(tempDS); 
    } 
    //determine order of files submitted 
    var productIndex = dataSets[0].DataSetName == "products" ? 0:1; 
    var languageIndex = dataSets[0].DataSetName == "products" ? 1:0; 
    var joined = from tProducts in dataSets[productIndex].Tables["product"].AsEnumerable() 
                 join tProductCountries in dataSets[productIndex].Tables["country"].AsEnumerable() on (int)tProducts["product_id"] equals (int)tProductCountries["product_id"] 
                 join tLanguageCountries in dataSets[languageIndex].Tables["country"].AsEnumerable() on (String)tProductCountries["country_text"] equals (String)tLanguageCountries["country_text"] 
                 join tLanguages in dataSets[languageIndex].Tables["language"].AsEnumerable() on (int)tLanguageCountries["language_Id"] equals (int)tLanguages["language_Id"] 
                  select new 
                  { 
                      Language = tLanguages["name"].ToString(), 
                      Product = tProducts["name"].ToString() 
                  }; 
 
    var listOfProducts = joined.OrderByDescending(_ => _.Product).Select(_ => _.Product).Distinct().ToList(); 
 
    foreach (var e in listOfProducts) 
    { 
        var e1 = e; 
        var languages = joined.Where(_ => _.Product == e1).Select(_ => _.Language).Distinct().ToList(); 
        languages.Sort(); 
        //Custom simple Array to text method 
        output.Add(String.Format("{0} {1}", e, ArrayToText(languages))); 
    } 
    return output; 
} 

这很好用,但我知道必须有更优化的解决方案来解决这个问题(尤其是当 XML 文件在现实生活中很大时)。有没有人有替代方法(linq 除外)的经验或关于优化当前方法的建议,这将使我更接近最佳解决方案?

非常感谢。

解决方案 实现建议的解决方案: Casperah 使用字典的方法在 312 毫秒内处理了数据集。 yamen 的方法使用 Linq Lookup 在 452ms 内处理了数据集。

请您参考如下方法:

您有两个问题,内存使用率和 CPU 使用率。

要限制内存使用,您可以使用 XmlReader,它只读取巨大的 xml 文件的一小部分。 要限制 CPU 使用率,您应该有国家代码索引。

我会这样做: 1. 阅读所有语言并将其插入字典,如下所示: //键是国家,值是语言列表。 词典>国家=新词典>(); 2. 使用 XmlReader 一次读取一个产品 3. 查找国家并写出语言可能使用哈希集来避免重复的语言。

这就是我的做法 - 祝你好运

我创建了这个例子:

        Dictionary<int, List<string>> countries = new Dictionary<int, List<string>>(); 
 
        XmlReader xml = XmlReader.Create("file://D:/Development/Test/StackOverflowQuestion/StackOverflowQuestion/Countries.xml"); 
        string language = null; 
        string elementName = null; 
        while (xml.Read()) 
        { 
            switch (xml.NodeType) 
            { 
                case XmlNodeType.Element: 
                    elementName = xml.Name; 
                    break; 
 
                case XmlNodeType.Text: 
                    if (elementName == "name") language = xml.Value; 
                    if (elementName == "country") 
                    { 
                        int country; 
                        if (int.TryParse(xml.Value, out country)) 
                        { 
                            List<string> languages; 
                            if (!countries.TryGetValue(country, out languages)) 
                            { 
                                languages = new List<string>(); 
                                countries.Add(country, languages); 
                            } 
                            languages.Add(language); 
                        } 
                    } 
                    break; 
            } 
        } 
        using (StreamWriter result = new StreamWriter(@"D:\Development\Test\StackOverflowQuestion\StackOverflowQuestion\Output.txt")) 
        { 
            xml = XmlReader.Create("file://D:/Development/Test/StackOverflowQuestion/StackOverflowQuestion/Products.xml"); 
            string product = null; 
            elementName = null; 
            HashSet<string> languages = new HashSet<string>(); 
            while (xml.Read()) 
            { 
                switch (xml.NodeType) 
                { 
                    case XmlNodeType.Element: 
                        elementName = xml.Name; 
                        break; 
 
                    case XmlNodeType.Text: 
                        if (elementName == "name") 
                        { 
                            if (product != null && languages != null) 
                            { 
                                result.Write(product); 
                                result.Write(" -> "); 
                                result.WriteLine(string.Join(", ", languages.ToArray())); 
                                languages.Clear(); 
                            } 
                            product = xml.Value; 
                        } 
                        if (elementName == "country") 
                        { 
                            int country; 
                            if (int.TryParse(xml.Value, out country)) 
                            { 
                                List<string> countryLanguages; 
                                if (countries.TryGetValue(country, out countryLanguages)) 
                                    foreach (string countryLanguage in countryLanguages) languages.Add(countryLanguage); 
                            } 
                        } 
                        break; 
                } 
            } 
        } 
    } 

它产生这个例子:

Screws -> English, French, Spanish 
Hammers -> Spanish, French 
Ladders -> English 
Wrenches -> English, French 

XmlReader.Create 需要一个 uri,您也可以使用类似:“http://www.mysite.com/countries.xml”