【发布时间】:2016-04-07 20:08:12
【问题描述】:
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());
}
}
}
}
}
我得到的错误是 Int.parse 无法转换为 .tostring?我究竟做错了什么。我正在尝试读取一个 xml 文件并将其发布到一个有五列的列表视图中。我有这个错误要修复并尝试只导入文件。
【问题讨论】:
-
你能包围在 try catch 中并发布堆栈跟踪吗?
-
XML 文件是什么样的?
-
确保您没有房地产中常见的 1/2 浴室或卧室。
-
@Craig 调试你的代码,看看错误发生时
House.Element("Bedrooms").Value的值是多少