简体   繁体   English

c#检索多个XML子节点

[英]c# Retrieve multiple XML child nodes

I've managed to link up a single XElement successfully into my program though I'm not having any luck with the other two I have in place, I've tried using; 我已经成功地将一个XElement链接到了我的程序中,尽管我尝试使用的其他两个没有运气;

IEnumerable query = from booking in doc.Descendants("Booking") IEnumerable查询=来自doc.Descendants(“ Booking”)中的预订

Though I've haven't had much luck placing the values into list box. 虽然我运气不太好,但是将这些值放入列表框。

Here's the code for function: 这是功能代码:

    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);
            }
        }
    }

If anyone knows where I've gone wrong or what I need to add / change please let me know :) 如果有人知道我哪里出了问题或需要添加/更改的内容,请告诉我:)

Many thanks, 10gez10 非常感谢,10gez10

You have several problems: 您有几个问题:

  1. Firstly, your data elements are not immediate children of the booking element, there are intermediate elements <Customer> , <Holiday> and <TravelInfo> . 首先,您的数据元素不是booking元素的直接子元素,其中包含中间元素<Customer><Holiday><TravelInfo> Thus you need to do something like 因此,您需要做类似的事情

      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. Secondly, several elements are misspelled: 其次,几个元素拼写错误:

    • CheckOutTime should be CheckoutTime CheckOutTime应该是CheckoutTime
    • In-FlightMeals should be InFlightMeals. 飞行中餐应该是飞行中餐。
    • CarHire should be CareHire (yes "CareHire" is what's in the XML.) CarHire应该是CareHire(是的,“ CareHire”是XML中的内容。)

    Thus, when you do (eg) Element("In-FlightMeals").Value , Element() is returning null so you get a null reference exception and your code is aborted. 因此,当您执行(例如) Element("In-FlightMeals").ValueElement()返回null,因此您将获得null引用异常,并且代码将中止。

  3. Thirdly, the element BookTo is completely missing, so BookTo = holiday.Element("BookTo").Value generates a null reference exception. 第三,元素BookTo完全丢失,因此BookTo = holiday.Element("BookTo").Value生成空引用异常。

More generally, I do not recommend this coding approach. 更一般而言,我不推荐这种编码方法。 If any of your XML elements are missing, your query will throw an exception because element.Element("name") will be null. 如果缺少任何XML元素,则查询将引发异常,因为element.Element("name")将为null。 What's worse, Visual Studio doesn't seem to report an accurate line number on which the null reference occurs, instead giving the line number of the select new statement. 更糟糕的是,Visual Studio似乎没有报告出现空引用的准确行号,而是给出了select new语句的行号。 And (on my version at least), it's not possible to step into the constructor for an anonymous type either. 而且(至少在我的版本中)也无法进入匿名类型的构造函数。 This makes debugging well-nigh impossible. 这使得调试几乎不可能。

Instead, skip the intermediate anonymous type and do things in a more direct, traditional manner: 而是跳过中间匿名类型并以更直接的传统方式执行操作:

            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.
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM