using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AS3_S5_CraigFenton
{
public partial class Form1 : Form
{
List<House> houseListings = new List<House>();
public Form1()
{
InitializeComponent();
}
private void buttongetListings_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
XElement root = XElement.Load(openFileDialog1.FileName);
foreach(var House in root.Elements("House"))
{
House h = new House();
h.HouseCode = House.Element("HouseCode").Value;
h.HouseType = House.Element("HouseType").Value;
h.Neighborhood = House.Element("HouseNeighborhood").Value;
h.Price = decimal.Parse(House.Element("Price").Value);
h.Bedrooms = int.Parse(House.Element("Bedrooms").Value);
houseListings.Add(h);
}
listViewlistings.Items.Clear();
var sortedHouse =
from House in houseListings
orderby House.HouseType, House.Price
select House;
foreach (House h in sortedHouse)
{
ListViewItem listingsItem = new ListViewItem();
listingsItem.Text = h.HouseCode;
listingsItem.SubItems.Add(h.HouseType);
listingsItem.SubItems.Add(h.Neighborhood);
listingsItem.SubItems.Add(h.Price.Tostring(0));
listingsItem.SubItems.Add(h.Bedrooms.Tostring());
}
}
}
}
}
My Error I am getting is Int.parse can not be converted to .tostring? What am I doing wrong. I am trying to read an xml file and post it to a listview that has five columns. I have this error to fix and trying to just import the file.
You may want to consider using the Int32.TryParse()
method which will evaluate and return a boolean to indicate if your value could be properly parsed or not :
int bedrooms;
if(Int32.TryParse(House.Element("Bedrooms").Value, out bedrooms))
{
// Your parse was successful, so set it
h.Bedrooms = bedrooms;
}
else
{
// Otherwise it wasn't in the correct format (a breakpoint
// here would be useful
}
It's likely that the use of any room numbers however might be expressed as a double (eg 2.5 Bedrooms or 3.5 Bathrooms) so you might want to consider how you might handle these as those are common scenarios.
Additionally, you have a few calls to .Tostring()
, which you should ensure that you use the propercasing .ToString()
to avoid any compiler issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.