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 8257097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:05:10+00:00 2026-06-08T02:05:10+00:00

Kinda stuck on this one. I have a table with name/value columns: NameValue table

  • 0

Kinda stuck on this one. I have a table with name/value columns:

NameValue table
-----------------------------------------------------------
Name varchar(100)    Value varchar(100)
-----------------------------------------------------------
FirstName            First value
SecondName           Second value
ThirdName            Null or Empty String
etc...            

I’m trying to get my result to look like the following XML, but I can’t quite get there.

<MyValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FirstName>First value</FirstName>
  <SecondName>Second value</SecondName>
  <ThirdName xsi:nil="true" />
</MyValues>

To get the dynamic names working, I concat the XML and cast as XML like so:

select cast('<' + name +  '>' + value + '</' + name + '>' as xml)
from NameValue
for xml raw(''), root('MyValues'), elements xsinil

The above query produces this XML without xsi:nil="true"

<MyValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FirstName>First value</FirstName>
  <SecondName>Second value</SecondName>
  <ThirdName />
</MyValues>

If I don’t cast as XML, I’m left with the following:

<MyValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
&lt;FirstName&gt;First value&lt;/FirstName&gt;&lt;
etc...

I tried adding the xsi:nil="true" in the concatination, but receive errors about the missing namespace. I’m guessing to make this work, I’m going to have to add the same namespace to every single row that contains null or empty string, so the result would look like the following:

<MyValues>
  <FirstName>First value</FirstName>
  <SecondName>Second value</SecondName>
  <ThirdName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
</MyValues>

There could be a few hundred of these empty strings in the resultset, so I’d prefer to put the namespace at the root level to conserve on bandwidth. Is this at all possible?

  • 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-08T02:05:11+00:00Added an answer on June 8, 2026 at 2:05 am

    Instead of using FOR XML, I simply did the entire XML as a concatinated string.

    declare @xml varchar(max)
    set @xml = '<MyValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
    
    select @xml = '<' + name +  
        case when len(value) = 0 then 'xsi:nil="true" />'
             else '>' + value + '</' + name + '>'
        end 
    from NameValue 
    
    select @xml + "</MyValues>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm kinda stuck with this one so I hoped someone could help me. I
I'm kinda stuck on this decision. My project already uses Spring and Spring Blazeds
Kinda stuck here... I have an application with lets say 5000 rows of data
It's amazing how fast you start using this framework, but i'm stuck on one
Im kinda stuck. I have an activity in my app that just wont go
I have a form with a few fieldsets. One fieldset has a table of
i'm kinda new to Objective-C so i have been learning with the Head First
I have a table of of more then 100 rows. each row consist of
I have this LINQ query (sorry it;s a long one) that query sets from
I kind of stuck on how to implement this query - this is pretty

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.