正在填充父级 + SQL Server 2008 中的子对象(通过 XML?)

发布于 2024-09-14 01:05:10 字数 2658 浏览 3 评论 0原文

我有一个简单的 DAL,它由一个 SalesEnquiry 对象组成,其中包含一个 Vehicle 对象的 List,用于处理传入的查询(XML)并将它们写入数据库。到目前为止,一切都很好。

但是,我正在编写另一个应用程序来进一步处理此数据库中的数据,因此我想使用这些相同的 DAL 对象来检索和操作数据。

我认为我可以将 SQL Server 中的数据作为 XML 返回并反序列化,而不是返回传统的记录集并迭代它们,手动填充 SalesEnquiry/Vehicle 对象的每个属性。我已经使用这种技术来处理传入的数据。

但是,我不确定是否/如何在 SQL Server 中构造适当的 XML,或者是否必须分两个阶段进行。

以下将把查询提取为 XML:

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

以下将把关联的车辆提取为 XML:

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

我所追求的结果 XML 是:

<?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>

我可以在 SQL Server 中创建它,还是必须在我的 DAL 中手动执行此操作?

更新:

根据 Shunty 的建议,我正在使用以下内容:

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

..我可以接近,但我还没有完全实现:

<?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>

要成功反序列化,我需要我的 ; 元素位于 父元素下。必须有一种方法来鼓励 SQL 制作正确的 XML...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-09-21 01:05:11

使用 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

返回:

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

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>
找回味觉 2024-09-21 01:05:11

使用标准内部联接,然后查看 AUTO ( FOR XML 子句的关键字(在 MSDN 上)。我还没有完全尝试过,但它看起来确实是你想要的。

Use a standard inner join and then take a look at the AUTO (and ELEMENTS) keywords (on MSDN) for your FOR XML clause. I haven't tried it exactly but it certainly looks to be what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文