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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:04:24+00:00 2026-05-13T12:04:24+00:00

I need to read in XML data posted from external systems, which will be

  • 0

I need to read in XML data posted from external systems, which will be formatted roughly as follows:

<Applicant>
  <FirstName>John</FirstName>
  <LastName>Smith</LastName>
  <Address>12 Main St</Address>
</Applicant>

This is a direct mapping of my Linq to SQL Applicant class, excluding a few properties.

What’s the best way to deserialize xml into a Linq to SQL object, so I can then insert directly into my database? I’d also like to validate the incoming XML and handle specific errors if possible.

Thanks in advance!

  • 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-13T12:04:25+00:00Added an answer on May 13, 2026 at 12:04 pm

    If it is a direct map, you should just be able to use it directly, as long as the types as public and have public parameterless constructors, and the properties (including lists) are get/set.

    If you need to tweak the names there is an XmlSerializer constructor that allows you to specify all the attributes. This is ideal for your scenario, but you must cache and re-use the serializer if you use this constructor overload, otherwise you will leak memory (the dynamic assemblies are not collected).

    Here’s a full example that removes one property (XmlIgnore), changes another to an attribute, and leaves a third as an element.

    using System;
    using System.IO;
    using System.Xml.Serialization;
    public class Foo
    {
        public int A { get; set; }
        public string B { get; set; }
        public int C { get; set; }
    }
    static class Program
    {
        static readonly XmlSerializer serializer;
        static Program()
        {
            XmlAttributeOverrides or = new XmlAttributeOverrides();
            or.Add(typeof(Foo), "A", new XmlAttributes { // change to an attrib
                XmlAttribute = new XmlAttributeAttribute("tweaked")
            });
            or.Add(typeof(Foo), "B", new XmlAttributes {
                XmlIgnore = true // turn this one off
            });
            // leave C as a default named element
            serializer = new XmlSerializer(typeof(Foo), or);
        }
        static void Main()
        {
            Foo foo = new Foo { A = 123, B = "def", C = 456 }, clone;
            string xml;
            using (StringWriter sw = new StringWriter())
            {
                serializer.Serialize(sw, foo);
                xml = sw.ToString();
            }
            using (StringReader sr = new StringReader(xml)) {
                clone = (Foo)serializer.Deserialize(sr);
            }
            Console.WriteLine(xml);
            Console.WriteLine();
            Console.WriteLine(clone.A);
            Console.WriteLine(clone.B);
            Console.WriteLine(clone.C);
        }
    }
    

    Note also that if you only need to change things at the type level (such as [XmlInclude]) then you can do this via the partial class that LINQ-to-SQL generates; for example:

    namespace My.Dal.Namespace {
        // add a type attribute to SomeEntity
        [XmlInclude(typeof(SomeDerivedEntity))]
        partial class SomeEntity { } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 308k
  • Answers 308k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The code examples for Erica Sadun's rather excellent 3.0 edition… May 13, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer First things first: it would probably be a lot more… May 13, 2026 at 9:46 pm
  • Editorial Team
    Editorial Team added an answer I have been evaluating all commercial engines for almost 1… May 13, 2026 at 9:46 pm

Related Questions

I have a noob LINQ to XML question. I have xml like so: <pages>
I'm writing out some xml from C# using the .net framework's XmlTextWriter. This works
I need to control the data type when reading XML data in SAS. The
I am using a class library which represents some of its configuration in .xml.
I've been experimenting with using MS Excel 2007 to edit tabular data stored in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.