c# Retrieve multiple XML child nodes -


i've managed link single xelement program though i'm not having luck other 2 have in place, i've tried using;

ienumerable query = booking in doc.descendants("booking")

though i've haven't had luck placing values list box.

here's code 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 customer elements                 var query = 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 of 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 knows i've gone wrong or need add / change please let me know :)

many thanks, 10gez10

you have several problems:

  1. firstly, data elements not immediate children of booking element, there intermediate elements <customer>, <holiday> , <travelinfo>. need like

            var query = 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 misspelled:

    • checkouttime should checkouttime
    • in-flightmeals should inflightmeals.
    • carhire should carehire (yes "carehire" what's in xml.)

    thus, when (e.g.) element("in-flightmeals").value, element() returning null null reference exception , code aborted.

  3. thirdly, element bookto missing, bookto = holiday.element("bookto").value generates null reference exception.

more generally, not recommend coding approach. if of xml elements missing, query throw exception because element.element("name") null. what's worse, visual studio doesn't seem report accurate line number on null reference occurs, instead giving line number of select new statement. , (on version @ least), it's not possible step constructor anonymous type either. makes debugging well-nigh impossible.

instead, skip intermediate anonymous type , things in 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;                 }                 // , on.             } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -