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

  • Home
  • SEARCH
  • 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 8526829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:23:04+00:00 2026-06-11T08:23:04+00:00

I’m using SQLCommand in C# to run SQL against a local machine which will

  • 0

I’m using SQLCommand in C# to run SQL against a local machine which will output XML. The SQL code looks like this:

"SELECT 
   ' '                          AS 'ItemsCount', 
   ' '                          AS 'Shipping', 
   ' '                          AS 'Fee', 
   ' '                          AS 'ShippingPrc', 
   ' '                          AS 'FeeType', 
   ' '                          AS 'FeeTaxPrc', 
 //many lines of SQL omitted
 FROM   im_item 
   JOIN ps_doc_lin 
     ON im_item.item_no = ps_doc_lin.item_no 
   JOIN  ps_doc_hdr 
     ON ps_doc_hdr.doc_id = ps_doc_lin.doc_id 
   JOIN ar_cust 
   ON ar_cust.cust_no = ps_doc_hdr.cust_no 
 WHERE  ps_doc_hdr.tkt_dt = (SELECT Max(tkt_dt) //I select Max(tkt_dt) here because this program will run right after a customer has completed checkout.  I want to get the most recent ticket (i.e. the ticket just rang up)
                        FROM   ps_doc_hdr) 
 FOR xml path ('Receipt')";

This has been working but I recently discovered a fatal flaw. If in the database a customer has purchased multiple items, the XML output from SQL Server will be an XML file with that customer’s information repeated for each item purchased. If I parse the XML output in C# it can’t be done, the output results in multiple root elements. I suspect that the JOIN of the various tables is causing this but I am having to pull a variety of data from SQL and I need these joins to get to what I need.

I tried to do something similar to this:

   ' '                          AS 'Total', 
   ' '                          AS 'InvcHdrRcptStatus', 
   ' '                          AS 'InvcHdrRcptType', 
   ' '                          AS 'Cashier', 
   ' '                          AS 'DocDate', 
   ' '                          AS 'InvcNum', 
   (SELECT      ps_doc_lin.sls_rep           AS 'Clerk'
                    FROM PS_DOC_LIN
                    WHERE PS_DOC_LIN.DOC_ID = PS_DOC_HDR.DOC_ID
                    FOR XML PATH ('Item')) as Items, 
   (SELECT     ar_cust.cust_no              AS 'BillToCustNumber', 
   ' '                          AS 'BillToCustCompany', 
   ar_cust.fst_nam              AS 'BillToFName', 
   ar_cust.lst_nam              AS 'BillToLName', 
   ar_cust.salutation           AS 'Customer/Name/Title', 
   ar_cust.adrs_1               AS 'BillToAddr1', 
   ar_cust.adrs_2               AS 'BillToAddr2', 
   ' '                          AS 'BillToAddr3', 
   ar_cust.zip_cod              AS 'BillToZip', 
   ' '                          AS 'BillToInfo1', 
   ' '                          AS 'BillToInfo2', 
   ' '                          AS 'BillToPhone1', 
   ' '                          AS 'BillToPhone2', 
   ar_cust.phone_1              AS 'ShipToPhone1', 
   ar_cust.phone_2              AS 'ShipToPhone2' FROM AR_CUST WHERE AR_CUST.CUST_NO = PS_DOC_HDR.CUST_NO FOR XML PATH ('Customer'), TYPE) as Customers,

I nested the JOIN into another select. While this did work, it didn’t format correctly when converted to JSON. The output looks like this:

{"Receipt":{ ......//omitted data
 ,"Customer":{"Name: {"title":"Mr."}}

My intended output would have all of the data organized inside of {“Receipt”: without the use of another {. Similar to this:

    "Receipt" : {
    "InvcHdrNotes" : ""
    "Tax" : ""
    "TaxPrc" : ""
    "DiscPrc" : ""
    "Discount" : ""
    "InvcComment1" : ""
    "InvcComment2" : ""
}

The “Receipt” is a subsection of a much larger JSON file. However, this flaw will be replicated (I assume so) for the remainder of the data I need to get since I will need JOIN for those as well. I have tried using both Jayrock and Newtonsoft.Json but both have this issue.

My Goal XML output would be:

<Receipt>
<InvcHdrNotes> </InvcHdrNotes>
<Tax>Y</Tax>
<Clerk>MGR</Clerk>
<BillToCustNumber>1000</BillToCustNumber>
<BillToCustCompany> </BillToCustCompany>
<BillToFName>Bill</BillToFName>
<BillToLName>Baker</BillToLName>
<Customer>
  <Name>
    <Title>Mr.</Title>
  </Name>
</Customer>
<BillToAddr1>1426 Millstream Parkway</BillToAddr1>
<BillToAddr3> </BillToAddr3>
<BillToZip>38120</BillToZip>
<BillToInfo1> </BillToInfo1>
<BillToInfo2> </BillToInfo2>
<ShipToCustNumber>1000</ShipToCustNumber>
<ShipToCustCompany> </ShipToCustCompany>
<ShipToFName>Bill</ShipToFName>
<ShipToLName>Baker</ShipToLName>
<ShipToTitle>Mr.</ShipToTitle>
<ShipToAddr1>1426 Millstream Parkway</ShipToAddr1>
<ShipToZip>38120</ShipToZip>
</Receipt>

I get this output with the SQL code above but the identical information is repeated after the which throws the exception I mentioned.

Is this even possible or is this a fatal design flaw? If it isn’t possible, I am unsure of how to complete this task. Thank you.

  • 1 1 Answer
  • 0 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-06-11T08:23:06+00:00Added an answer on June 11, 2026 at 8:23 am

    It looks like you’re serializing each row returned to the same XML – so you would end up with multiple root elements. You need to serialize the List itself.

    So you’d end up with:

    <Receipts>
      <Receipt>
        ...
      </Receipt>
      <Receipt>
        ...
      </Receipt>
    </Receipts>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.