Today i discover that it is much neat and fast to parse an XML String into Object list when I utilized the System.XML.Linq.
here is the code to demostrate this, you can create a console app to see how it works.
class Program
{
static void Main(string[] args)
{
string xmlString = @"<FCDetail><FCDetail><FC> ZRL - 02 </FC><FCDesc> FC Description </FCDesc>
<FGC> FAC </FGC><FGroup> Accessories </FGroup></FCDetail></FCDetail><FCDetailMessage></FCDetailMessage>";
string xml = "<FCRoot>" + xmlString + "</FCRoot>";
var xDoc = XDocument.Parse(xml);
var listNode = xDoc.Root.Elements("FCDetail").FirstOrDefault();
List<FCodeModel> list = new List<FCodeModel>();
foreach (var fCodeElement in listNode.Elements("FCDetail"))
{
list.Add(new FCodeModel
{
FC =fCodeElement.Elements("FC").Select(e => e.Value).FirstOrDefault() ?? "",
FCDesc =fCodeElement.Elements("FCDesc").Select(e => e.Value).FirstOrDefault() ?? "",
FGC =fCodeElement.Elements("FGC").Select(e => e.Value).FirstOrDefault() ?? "",
FGroup =fCodeElement.Elements("FGroup").Select(e => e.Value).FirstOrDefault() ?? ""
});
}
}
public class FCodeModel
{
[JsonProperty("fCode")]
public string FC { get; set; }
[JsonProperty("fCodeDesc")]
public string FCDesc { get; set; }
[JsonProperty("fGroupCode")]
public string FGC { get; set; }
[JsonProperty("FGroup")]
public string FGroup { get; set; }
}
a blog to share Microsoft technologies Azure, DotNet, SharePoint, SQL Server,JavaScript Framework: Node.JS, Angular, React
Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts
Wednesday, April 19, 2017
Wednesday, January 15, 2014
How to improve the performance of Linq IEnumerable to Object List
I found an issue with the my linq code. it is straight forward code.
it simply call a Ienumerable object to list
List<myObject>=myIenumerable.ToList<myObject>();
as the number of objects in the Ienumerable Increases. the app take noticeable time to load the data.
the good search return the following will provide a great help to solve the issue.
http://msmvps.com/blogs/jon_skeet/archive/2011/01/01/reimplementing-linq-to-objects-part-20-tolist.aspx
I changed my code to
List<myObject>=new List<myObject>(myIenumerable);
the performance take a big jump.
List<myObject>=myIenumerable.ToList<myObject>();
List<myObject>=new List<myObject>(myIenumerable);
the performance take a big jump.
Subscribe to:
Posts (Atom)