Saturday, 10 September 2016

Create List from xml file

I am sharing  simple code to create List  from xml file using the ASP.Net C#.

public static List<State> GettimeZoneByState(string StateCode)
XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Content/States.xml"));
    var stateCodeData = doc.Root.Elements("state");

    var timezone = stateCodeData.Select(a => new State{
                                   StateName = a.Element("name").Value,
                                   StateCode = a.Element("abbreviation").Value, 
                                   TimeZone2 = a.Element("time-zone-2").Value, 
                                   Timezone = a.Element("time-zone-1").Value 
    return timezone;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.