Torvos Torvos - 1 year ago 103
C# Question

C# populating class array from xml linq results in FormatException

I'm getting a FormatException error "Input string was not in a correct format." error...

Employee class is all strings except the Status(int), Type(int) and Displayed(bool)

public partial class Version2 : System.Web.UI.Page {

private Employee[] Employees;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XDocument xdoc = XDocument.Load(Server.MapPath("~/")+ "V2Employees.xml");
IEnumerable<XElement> emps = xdoc.Root.Descendants();

Employees = emps.Select(x => new Employee()
{
Employee_ID = x.Attribute("id").Value,
First_Name = x.Element("First_Name").Value,
Last_Name = x.Element("Last_Name").Value,
Classification_Group = x.Element("Classification_Group").Value,
Classification_Level = x.Element("Classification_Level").Value,
Postion_Number = x.Element("Postion_Number").Value,
English_Title = x.Element("English_Title").Value,
French_Title = x.Element("French_Title").Value,
Location = x.Element("Location").Value,
Language = x.Element("Language").Value,
Department_ID = x.Element("Department_ID").Value,
Status = int.Parse(x.Element("Status").ToString()),
Type = int.Parse(x.Element("Type").ToString()),
Displayed = false}).ToArray();
}


Example XML going in:

<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee id="1">
<First_Name>Jane</First_Name>
<Last_Name>Doe</Last_Name>
<Classification_Group>AA</Classification_Group>
<Classification_Level>02</Classification_Level>
<Postion_Number>12345</Postion_Number>
<English_Title>Bob</English_Title>
<French_Title></French_Title>
<Location>Null Island</Location>
<Language>English</Language>
<Department_ID>000001</Department_ID>
<Status>1</Status>
<Type>4</Type>
</employee>
</employees>

Answer Source

See code below. The fix was : xdoc.Root.Descendants("employee"); I also made other improvements

    public class Employee
    {
        static Employee[] Employees = null;

        const string FILENAME = @"c:\temp\test.xml";
        public static Boolean IsPostBack = false;

        string Employee_ID { get; set; }
        string First_Name { get; set; }
        string Last_Name { get; set; }
        string Classification_Group { get; set; }
        string Classification_Level { get; set; }
        string Postion_Number { get; set; }
        string English_Title { get; set; }
        string French_Title { get; set; }
        string Location { get; set; }
        string Language { get; set; }
        string Department_ID { get; set; }
        int Status { get; set; }
        int Type { get; set; }
        Boolean Displayed { get; set; }


        //protected void Page_Load(object sender, EventArgs e)
        public void Page_Load()
        {
            if (!IsPostBack)
            {
                XDocument xdoc = XDocument.Load(FILENAME);
                IEnumerable<XElement> emps = xdoc.Root.Descendants("employee");

                Employees = emps.Select(x => new Employee()
                {
                    Employee_ID = (string)x.Attribute("id"),
                    First_Name = (string)x.Element("First_Name"),
                    Last_Name = (string)x.Element("Last_Name"),
                    Classification_Group = (string)x.Element("Classification_Group"),
                    Classification_Level = (string)x.Element("Classification_Level"),
                    Postion_Number = (string)x.Element("Postion_Number"),
                    English_Title = (string)x.Element("English_Title"),
                    French_Title = (string)x.Element("French_Title"),
                    Location = (string)x.Element("Location"),
                    Language = (string)x.Element("Language"),
                    Department_ID = (string)x.Element("Department_ID"),
                    Status = (int)x.Element("Status"),
                    Type = (int)x.Element("Type"),
                    Displayed = false
                }).ToArray();
            }
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download