簡體   English   中英

如何使用 .net 3.5/C# 簡化此代碼

[英]How to simplify this code using .net 3.5/C#

我在C# and .net 3.5中有以下代碼工作正常,但需要知道這是否可以簡化可能是使用 Linq 或其他什么? 基本上我正在閱讀 xml 文件來獲取列名。 然后嘗試復制字典 object 中的列和序列屬性,如果“列的 isactive 屬性為”真“。我在代碼的其他部分使用這個字典 object。我循環遍歷元素,然后遍歷屬性並查找列那些是活動的,如果活動我存儲序列,最后將列和序列添加到字典中。

var doc = XDocument.Load("DataStructure.xml");
var cols = doc.XPathSelectElements("/datastructure/" + sPageName + "/columns");
Dictionary<string, int> columns = new Dictionary<string, int>();
bool bAddData = true;
int sSequence = 0;

foreach (var col in cols.Elements())
{
    foreach (XAttribute at in col.Attributes())
    {
        if (at.Name.ToString().ToLower().Equals("isactive") && at.Value.ToString() != "true")
        {
            bAddData = false;
            break;
        }

        bAddData = true;                    
        if (at.Name.ToString().ToLower().Equals("sequence"))
        {
            sSequence = Convert.ToInt32(at.Value.ToString());
            break;
        }
    }
    if (bAddData)
    {
        columns.Add(col.Name.ToString(), sSequence);
    }
}

我確信這是非常糟糕的代碼,但我想知道如何改進它。 這是 xml 數據文件。 如果我需要重組 xml 以使其變得簡單,我可以。

<?xml version="1.0" encoding="utf-8" ?>
<datastructure>
  <MyPage>
    <columns>
      <number isactive="true" sequence="1" />
      <curr_code isactive="true" sequence="2" />
      <curr_desc isactive="true" sequence="3" />
      <tradecurrvalue isactive="true" sequence="4" />
      <selectcurrvalue isactive="true" sequence="5" />
    </columns>    
  </MyPage>
</datastructure>

我認為應該這樣做:

var doc = XDocument.Load("DataStructure.xml");
var cols = doc.XPathSelectElements("/datastructure/" + sPageName + "/columns");

Dictionary<string, int> columns =
  cols.Elements()
  .Where(c => c.Attribute("isactive").Value == "true")
  .ToDictionary(
    c => c.Name.ToString(),
    c => Int32.Parse(c.Attribute("sequence").Value)
  );

編輯:

出於好奇,一旦我弄清楚它究竟做了什么以及如何最好地使用XElement class 中的方法,我想知道在不使用 LINQ 的情況下可以對原始代碼做什么,我得出了這個結論:

var doc = XDocument.Load("DataStructure.xml");
var cols = doc.XPathSelectElements("/datastructure/" + sPageName + "/columns");
Dictionary<string, int> columns = new Dictionary<string, int>();

foreach (var col in cols.Elements()) {
  if (col.Attribute("isactive").Value == "true") {
    columns.Add(col.Name.ToString(), Int32.Parse(col.Attribute("sequence").Value));
  }
}
var doc = XDocument.Load("DataStructure.xml");
var cols = doc.XPathSelectElements("/datastructure/MyPage/columns").Descendants();
var columns = cols.Where(e => e.Attribute("isactive").Value.ToLower() == "true").ToDictionary(e => e.Name, e => int.Parse(e.Attribute("sequence").Value));

你可以試試這樣的

cols.Elements().Where(
                e =>
                    {
                        var attribute = e.Attribute("isactive");
                        return attribute != null &&
                               attribute.Value.ToString().Equals("true", StringComparison.CurrentCultureIgnoreCase);
                    })
                .ToDictionary(e => e.Name.ToString(),
                              e => Convert.ToInt32(e.Attribute("sequence").Value.ToString()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM