Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:19:07+00:00 2026-05-16T06:19:07+00:00

I have simple DAL that consists of a SalesEnquiry object, which includes a List<T>

  • 0

I have simple DAL that consists of a SalesEnquiry object, which includes a List<T> of a Vehicle object, which is used to process incoming enquiries (XML) and write them to a DB. So far so good.

However, I’m writing another app that further processes data in this DB, so I’m wanting to use these same DAL objects to retrieve and manipulate the data.

Rather than returning conventional recordsets and iterating through them, manually populating each property of the SalesEnquiry/Vehicle objects, I thought I could return the data from SQL Server as XML and de-serialise it. I already use this technique to handle the incoming data in the first place.

However, I’m not sure if/how I can construct the appropriate XML in SQL Server or if I must do it in two stages.

The following will extract the enquiries as XML:

Select EnquiryID as 'enquiry/enquiryid',
     EnquiryNo as 'enquiry/enquiryno',
     CompanyName as 'enquiry/company'
From Enquiries e
Where e.EnquiryID = 23
For XML PATH

And the following will extract the associated vehicles as XML:

Select VehicleID as 'vehicle/vehicleid',
    VehicleReg as 'vehicle/vehiclereg'
From Vehicles v
Where v.EnquiryID= 23
For XML PATH

The resulting XML I am after is:

<?xml version="1.0" encoding="utf-8"?>
<enquiry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <enquiry_id>123</enquiry_no>
  <enquiry_no>100004</enquiry_no>
  <company>MyCompany</company>
  <enquiry_no>100004</enquiry_no>
  <vehicles>
    <vehicle>
      <vehicle_registration>ABC123</vehicle_registration>
    </vehicle>
    <vehicle>
      <vehicle_registration>XYZ789</vehicle_registration>
    </vehicle>
  </vehicles>
</enquiry>

Can I create this in SQL Server, or must I manually do this in my DAL?

Update:

Following on from Shunty’s suggestions, I’m using the following:

Select enquiry.EnquiryID as enquiry_id, enquiry.EnquiryNo, enquiry.CompanyName, VehicleID as [vehicle.vehicle_id], VehicleReg as [vehicle.vehicle_registration]
From Enquiries as enquiry
inner join Vehicles on Vehicles.EnquiryID = enquiry.EnquiryID
Where enquiry.EnquiryID = 23
For XML AUTO, ELEMENTS

..I can get close, but I’m not quite there:

<?xml version="1.0" encoding="utf-8"?>
<enquiry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <enquiry_id>123</enquiry_no>
  <enquiry_no>100004</enquiry_no>
  <company>MyCompany</company>
  <enquiry_no>100004</enquiry_no>
    <vehicle>
      <vehicle_registration>ABC123</vehicle_registration>
    </vehicle>
    <vehicle>
      <vehicle_registration>XYZ789</vehicle_registration>
    </vehicle>
</enquiry>

To successfully deserialize, I need my <vehicle> elements to come under a <vehicles> parent element. There must be a way to encourage SQL to craft the right XML…

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:19:07+00:00Added an answer on May 16, 2026 at 6:19 am

    An alternative using XML PATH:

    SELECT
      EnquiryID AS enquiry_id,
      EnquiryNo AS enquiry_no,
      CompanyName AS company,
      (
        SELECT
          VehicleReg AS vehicle_registration
        FROM Vehicles
        WHERE EnquiryID = e.EnquiryID
        FOR XML PATH ('vehicle'), TYPE, ROOT('vehicles')
       )
    FROM Enquiries e 
    WHERE EnquiryID = 123
    FOR XML PATH ('enquiry'), TYPE
    

    Returns:

    <enquiry>
      <enquiry_id>123</enquiry_id>
      <enquiry_no>100004</enquiry_no>
      <company>MyCompany</company>
      <vehicles>
        <vehicle>
          <vehicle_registration>ABC123</vehicle_registration>
        </vehicle>
        <vehicle>
          <vehicle_registration>XYZ789</vehicle_registration>
        </vehicle>
      </vehicles>
    </enquiry>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent object (part of a DAL) that contains, amongst other things,
I have simple class with width and height member fields which define number of
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass
We are working on improving our DAL which is written in LINQ that talks
We have a DAL that we're using with NHibernate.Search , so classes that need
I have a simple question that I think someone will answer very fast, but
I have a simple database that I use, with an EF data model to
I have a DAL method that retrieves items. The items exist in the DAL
We have a highly specialized DAL which sits over our DB. Our apps need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.