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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:48:02+00:00 2026-05-13T10:48:02+00:00

Is there a simpler way to output a Linq to SQL table to XML

  • 0

Is there a simpler way to output a Linq to SQL table to XML (eventually to a web page?) Unfortunately, I am not using MVC, but I could put a reference to it in my aspx C# page.

I have this:

var myView = (from x in db.myTable 
              where x.Name.Contains("Bob") 
              select new person {Name = x.Name, Job = x.Job).Take(100);

and want to output something similar to this (doesn’t have to be exact for now):

<?xml version="1.0" encoding="utf-8"?> 
<myView xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <person><Name>Bob Smith</Name><Job>Machinist</Job></person>
    <person><Name>Bob Smithers</Name><Job>Cartoonist</Job></person>
    <person><Name>Rebob Wilson</Name><Job>Mason</Job></person>
</myView> 

I tried doing this:

TextWriter myTW = new StreamWriter( Response.OutputStream, Encoding.UTF8);
XmlTextWriter xmlTW = new XmlTextWriter(myTW);
XmlSerializer myS = new XmlSerializer( typeof( person));
myS.Serialize( xmlTW, myView);

But I get a “Cannot serialize iQueryable”. However, I tried ToArray, ToList, AsEnumerable, etc. and simply get “An error occured” with myS.Serialize().

Any thoughts? Hopefully there is a way like return XML(myView);

  • 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-05-13T10:48:02+00:00Added an answer on May 13, 2026 at 10:48 am

    Try this:

    var result = myView.ToArray();
    var serializer = new XmlSerializer(result.GetType());
    serializer.Serialize(Response.OutputStream, result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there simpler way to convert CSV files to SQL file than this?: BufferedReader
When you have a derived class, is there an simpler way to refer to
Is there a better/simpler way to find the number of images in a directory
It seems like there should be a simpler way than: import string s =
I was wonder if there is a simpler (single) way to calculate the remaining
i am investigating into the way LINQ gets mapped to SQL, and I have
Is there a simpler way of implement this? Or a implemented method in JDK
I've discovered a very nasty gotcha with Linq-to-sql, and i'm not sure what the
Is there a simple way of getting a HTML textarea and an input type=text
Is there a simple way to cache MySQL queries in PHP or failing that,

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.