运行应用程序时出现空白表单

时间:2016-06-28 12:16:35

标签: c#

我实际上是在尝试运行应用程序来读取xml文件然后在表单中显示。但是,它不断给我一个没有数据的空白表格。我将如何实际显示订单数据?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ReadOrder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            StringBuilder orderList = new StringBuilder();
            orderList.Append("Order List:").Append(Environment.NewLine);
            int counter = 0;
            // location of xml file
            string xmlFilePath = @"..\..\Orders.xml";

            {
                // get reference to XmlReader object
                XmlReader reader = XmlReader.Create(xmlFilePath);
                while (reader.ReadToFollowing("order"))
                {
                    counter++;
                    orderList.Append("Order Counter: " + counter + Environment.NewLine);
                    reader.ReadToFollowing("Item");
                    int itemCount = 1;
                    orderList.Append("Item: " + itemCount + reader.ReadElementContentAsString() +
                        Environment.NewLine);

                    while (reader.ReadToNextSibling("item"))
                    {
                        itemCount++;
                        orderList.Append("item: " + itemCount + reader.ReadElementContentAsString()
                            + Environment.NewLine);
                    }
                    reader.ReadEndElement();
                }

                reader.Close();
                Console.WriteLine(orderList);
                Console.Read();


            }
        }
    }
}




<?xml version="1.0" encoding="utf-8" ?>
<Orders>
  <order OrderID="1">
    <OrderDate>28/5/16</OrderDate>
    <BuyerId>2</BuyerId>
      <Item>
    <ItemId>100</ItemId>
    <ItemName>Memory Card</ItemName>
    <Description>300GB</Description>
    <Quantities>1</Quantities>
    <UnitPrice>50.00</UnitPrice>
    <Remarks>Nil</Remarks>
      </Item>
  </order>

  <order OrderID="2">
    <OrderDate>28/5/16</OrderDate>
    <BuyerId>4</BuyerId>
      <Item>
    <ItemId>101</ItemId>
    <ItemName>Samsung S6</ItemName>
    <Description>Black</Description>
    <Quantities>1</Quantities>
    <UnitPrice>700.00</UnitPrice>
    <Remarks>Nil</Remarks>
        </Item>
  </order>

  <order OrderID="3">
    <OrderDate>28/5/16</OrderDate>
    <BuyerId>6</BuyerId>
      <Item>
    <ItemId>102</ItemId>
    <ItemName>Samsung S7</ItemName>
    <Description>Gold</Description>
    <Quantities>1</Quantities>
    <UnitPrice>899.00</UnitPrice>
    <Remarks>Nil</Remarks>
       </Item>
  </order>

</Orders>

1 个答案:

答案 0 :(得分:1)

你错过了拦截障碍。对于每个try块,需要实现catch块。

try
{

}
catch(Exception)
{

}