2014-12-06 48 views
1

我已经成功,虽然我并没有与其他两个我在任何地方运气单一的XElement成功链接上我的节目,我已经尝试使用多个XML子节点;C#检索

IEnumerable的查询从doc.Descendants预订(“预订”)

虽然我还没有多少运气将值放入列表框中=。

下面是函数的代码:

private void btnimport_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog open = new OpenFileDialog(); 
     open.CheckFileExists = true; 
     open.InitialDirectory = "@C:\\"; 
     open.Filter = "XML Files (*.xml)|*.xml|All Files(*.*)|*.*"; 
     open.Multiselect = false; 

     if (open.ShowDialog() == DialogResult.OK) 
     { 
      try 
      { 
       XDocument doc = XDocument.Load(open.FileName); 

       //Grabs the customer elements 
       var query = from booking in doc.Descendants("Booking") 
       select new 
       { 
        //Customer Elements 
        CustomerId = booking.Element("CustomerId").Value, 
        Title = booking.Element("Title").Value, 
        Firstname = booking.Element("FirstName").Value, 
        Lastname = booking.Element("LastName").Value, 
        DateofBirth = booking.Element("DateofBirth").Value, 
        Email = booking.Element("Email").Value, 
        HouseNo = booking.Element("HouseNo").Value, 
        Street = booking.Element("Street").Value, 
        Postcode = booking.Element("Postcode").Value, 
        Town = booking.Element("Town").Value, 
        County = booking.Element("County").Value, 
        ContactNo = booking.Element("ContactNo").Value, 

        //Holiday Elements 
        HolidayId = booking.Element("HolidayId").Value, 
        HotelName = booking.Element("HotelName").Value, 
        Location = booking.Element("Location").Value, 
        BookFrom = booking.Element("BookFrom").Value, 
        BookTo = booking.Element("BookTo").Value, 
        CheckInTime = booking.Element("CheckInTime").Value, 
        CheckOutTime = booking.Element("CheckOutTime").Value, 
        NoOfRoomsBooked = booking.Element("NoOfRoomsBooked").Value, 
        RoomType = booking.Element("RoomType").Value, 
        RoomServices = booking.Element("RoomServices").Value, 
        Parking = booking.Element("Parking").Value, 
        Pet = booking.Element("Pet").Value, 

        //TravelInfo Elements 
        TravelInfoId = booking.Element("TravelInfoId").Value, 
        TravellingFrom = booking.Element("TravellingFrom").Value, 
        Destination = booking.Element("Destination").Value, 
        Fare = booking.Element("Fare").Value, 
        TravelInsurance = booking.Element("TravelInsurance").Value, 
        InFlightMeals = booking.Element("In-FlightMeals").Value, 
        LuggageAllowance = booking.Element("LuggageAllowance").Value, 
        ExtraLuggage = booking.Element("ExtraLuggage").Value, 
        CarHire = booking.Element("CarHire").Value, 
        ReturnTransfer = booking.Element("ReturnTransfer").Value, 
       }; 

       //Inputs all of the values in bookings 
       foreach (var booking in query) 
       { 
        //Customer values 
        txtCustomerId.Text = txtCustomerId.Text + booking.CustomerId; 
        txttitle.Text = txttitle.Text + booking.Title; 
        txtfname.Text = txtfname.Text + booking.Firstname; 
        txtlname.Text = txtlname.Text + booking.Lastname; 
        txtdob.Text = txtdob.Text + booking.DateofBirth; 
        txtemail.Text = txtemail.Text + booking.Email; 
        txthouseno.Text = txthouseno.Text + booking.HouseNo; 
        txtstreet.Text = txtstreet.Text + booking.Street; 
        txtpostcode.Text = txtpostcode.Text + booking.Postcode; 
        txttown.Text = txttown.Text + booking.Town; 
        txtcounty.Text = txtcounty.Text + booking.County; 
        txtcontactno.Text = txtcontactno.Text + booking.ContactNo; 

        //Holiday Values 
        txtHolidayId.Text = txtHolidayId.Text + booking.HolidayId; 
        txthname.Text = txthname.Text + booking.HotelName; 
        txtl.Text = txtl.Text + booking.Location; 
        txtbf.Text = txtbf.Text + booking.BookFrom; 
        txtbt.Text = txtbt.Text + booking.BookTo; 
        txtcit.Text = txtcit.Text + booking.CheckInTime; 
        txtcot.Text = txtcot.Text + booking.CheckOutTime; 
        txtnorb.Text = txtnorb.Text + booking.NoOfRoomsBooked; 
        txtrt.Text = txtrt.Text + booking.RoomType; 
        txtrs.Text = txtrs.Text + booking.RoomServices; 
        txtpark.Text = txtpark.Text + booking.Parking; 
        txtpet.Text = txtpet.Text + booking.Pet; 

        //TravelInfo Values 
        txtTravelInfoId.Text = txtTravelInfoId.Text + booking.TravelInfoId; 
        txttf.Text = txttf.Text + booking.TravellingFrom; 
        txtd.Text = txtd.Text + booking.Destination; 
        txtf.Text = txtf.Text + booking.Fare; 
        txtti.Text = txtti.Text + booking.TravelInsurance; 
        txtifi.Text = txtifi.Text + booking.InFlightMeals; 
        txtla.Text = txtla.Text + booking.LuggageAllowance; 
        txtel.Text = txtel.Text + booking.ExtraLuggage; 
        txtch.Text = txtch.Text + booking.CarHire; 
        txtrtrans.Text = txtrtrans.Text + booking.ReturnTransfer; 
       } 

       MessageBox.Show("XML has been imported"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 

如果有谁知道在那里我已经错了还是什么我需要添加/更改,请让我知道:)

非常感谢, 10gez10

+0

发生什么样的错误?您正在设置相同的属性,例如foreach-loop中的txtCustomerId.Text,这看起来很奇怪。 – 2014-12-06 01:11:45

+0

我有尝试和捕获异常的XML,我得到的错误是这个 - http://imgur.com/wbqeBwy。重复同一文本框的原因是语法xml.linq在未声明两次时抛出。没有理由为什么,但你 – 10AlexD10 2014-12-06 01:49:02

+0

@ 10gez10 - 你能分享你的XML吗?没有这个,它很难回答。 – 2014-12-06 03:21:41

回答

1

你有几个问题:

  1. 首先,你的数据元素不是booking元素的直接孩子,还有中间的元素<Customer><Holiday><TravelInfo>。因此,你需要做的是这样

     var query = from booking in doc.Descendants("Booking") 
            let customer = booking.Element("Customer") 
            let holiday = booking.Element("Holiday") 
            let travelInfo = booking.Element("TravelInfo") 
            select new 
            { 
             //Customer Elements 
             CustomerId = customer.Element("CustomerId").Value, 
             Title = customer.Element("Title").Value, 
             HolidayId = holiday.Element("HolidayId").Value, 
             TravelInfoId = travelInfo.Element("TravelInfoId").Value, 
            } 
    
  2. 第二,有几个要素拼错:

    • CheckOutTime应该CheckoutTime
    • InFlightMeals应该InFlightMeals。
    • CarHire应该CareHire(是“CareHire”是什么在XML)

    因此,当你这样做(例如)Element("In-FlightMeals").ValueElement()被返回null所以你得到一个空引用异常,你的代码是中止。

  3. 第三,元素BookTo完全丢失,所以BookTo = holiday.Element("BookTo").Value生成一个空引用异常。

更一般地说,我不推荐这种编码方法。如果缺少任何XML元素,则查询将引发异常,因为element.Element("name")将为空。更糟的是,Visual Studio似乎没有报告出现空引用的准确行号,而是给出了select new语句的行号。而且(至少在我的版本中),无法进入匿名类型的构造函数。这使得调试几乎不可能。

相反,跳过中间匿名类型,做事情更直接的,传统的方式:

  foreach (var booking in doc.Descendants("Booking")) 
      { 
       var customer = booking.Element("Customer"); 
       var holiday = booking.Element("Holiday"); 
       var travelInfo = booking.Element("TravelInfo"); 

       XElement element; 

       if (customer != null) 
       { 
        if ((element = customer.Element("CustomerId")) != null) 
         txtCustomerId.Text = txtCustomerId.Text + element.Value; 
       } 
       // And so on. 
      } 
+1

谢谢队友我通读你的反馈改变了它从匿名类型到foreach循环构造函数的建议,它适用于所有三个后代以及检查拼写错误等。此线程可以被关闭:)。 – 10AlexD10 2014-12-07 18:16:17