Thursday 30 May 2019

how to read resource file in .Net core

how to read resource file in .Net core


Description:

In this example we explain that how to read resource file in .Net core. Or how to read .resx file in .Net Core and MVC.we have requirement like read resource file dynamically and bind it to Dataset in .Net Core 2.1.so here we demonstrate how to reading a file stream from resource file in .Net Core.
Below is the code to demonstrate how to read and write XML or Resource file in .Net Core and bind the result in Dataset Table.

Code:

   public List<FormLanguage> PopulateGrid(string FormName,string LanguageValue)
        {
            this.dataSetControl.Clear();
            List<FormLanguage> lstFormLanguage = new List<FormLanguage>();
            bool IsResourceFileExist = false;
          
            if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "../RootPath/Resources/Views/" + FormName + "." + LanguageValue + ".resx")))
            {
                IsResourceFileExist = true;
                this.dataSetControlNames.ReadXml(Path.Combine(Directory.GetCurrentDirectory(), "../RootPath/Resources/Views/" + FormName + "." + LanguageValue + ".resx"));
            }
            else
            {
                this.dataSetControlNames.ReadXml(Path.Combine(Directory.GetCurrentDirectory(), "../RootPath/Resources/Views/" + FormName + ".nl-NL.resx"));
            }
        
            if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "../RootPath/Resources/Views/" + FormName +  "." + LanguageValue + ".resx")))
                this.dataSetControl.ReadXml(Path.Combine(Directory.GetCurrentDirectory(), "../RootPath/Resources/Views/" + FormName +  "." + LanguageValue + ".resx"));
            else
                this.dataSetControl.ReadXml(Path.Combine(Directory.GetCurrentDirectory(), "../RootPath/Resources/Views/" + FormName+ ".nl-NL.resx"));
          
            int tableCount = this.dataSetControl.Tables.Count;
            for (int i = 0; i < tableCount; i++)
            {
                if (this.dataSetControl.Tables[i].TableName.Equals("data"))
                {
                    //this.dataSetControl.Tables[i].DefaultView.RowFilter = "(value<>'' OR name LIKE '%.ToolTip%')  AND name NOT LIKE '%LinkButton%'";   //value <> ''
                    this.dataSetControl.Tables[i].DefaultView.RowFilter = "value<>'' AND name NOT LIKE '%LinkButton%'";
                    if (!this.dataSetControl.Tables[i].Columns.Contains("MultiLanguageValue"))
                    this.dataSetControl.Tables[i].Columns.Add("MultiLanguageValue");
                    for (int j = 0; j < this.dataSetControl.Tables[i].DefaultView.Count; j++)
                    {
                        string controlName = this.dataSetControl.Tables[i].DefaultView[j]["name"].ToString();
                        this.dataSetControlNames.Tables["data"].DefaultView.RowFilter = "name = '" + controlName + "'";
                        if (this.dataSetControlNames.Tables["data"].DefaultView.Count > 0)
                        {
                            this.dataSetControl.Tables[i].DefaultView[j]["MultiLanguageValue"] = this.dataSetControlNames.Tables["data"].DefaultView[0]["value"];
                        }
                    }
              DataView ds=      this.dataSetControl.Tables[i].DefaultView;
                    for (int k = 0; k < this.dataSetControl.Tables[i].Rows.Count; k++)
                    {
                        FormLanguage obj = new FormLanguage();
                        obj.Name = Convert.ToString(this.dataSetControl.Tables[i].Rows[k]["Name"]);
                        obj.Value = Convert.ToString(this.dataSetControl.Tables[i].Rows[k]["Value"]);
                        if(IsResourceFileExist)
                        obj.MultiLanguageValue = Convert.ToString(this.dataSetControl.Tables[i].Rows[k]["Value"]);
                        else
                            obj.MultiLanguageValue = Convert.ToString(this.dataSetControl.Tables[i].Rows[k]["Name"]);

                        obj.CultureId = LanguageValue;
                        obj.FormDescription = FormName;
                        lstFormLanguage.Add(obj);
                    }
                  
                }
            }
            return lstFormLanguage;
        }

0 comments:

Post a Comment