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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:36:54+00:00 2026-05-10T17:36:54+00:00

My question is the following. I have xml that is versioned by a namespace.

  • 0

My question is the following.

I have xml that is versioned by a namespace. I want to serialize this received xml into the appropriate object, but the only way I know to do this means I have to process the xml two times. First to discover the namespace, and then in order to serialize to the object of the proper type based on the discovered namespace. This seems dreadfully inefficient to me, and there must be some way using generics or something to get the appropriate type of object back without an ‘if namespace == x then serialze to that’ check.

Below is a sample of the only way I know to accomplish this. Is there a better or more efficient way?

Thanks

using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Xml.Linq; using System.Xml; using System.Xml.Serialization; using System.IO;  namespace TestProject1 {     [TestClass]     public class UnitTest1     {         [TestMethod]         public void TestMethod3()         {             //Build up an employee object to xml             Schema.v2.Employee employee = new Schema.v2.Employee { FirstName = 'First', LastName = 'Last' };             string xml = employee.ObjectToXml<Schema.v2.Employee>();              //Now pretend I don't know what type I am receiving.             string nameSpace = GetNamespace(xml);             Object newemp;             if (nameSpace == 'Employee.v2')                 newemp = XmlSerializationExtension.XmlToObject<Schema.v2.Employee>(null, xml);             else                 newemp = XmlSerializationExtension.XmlToObject<Schema.v1.Employee>(null, xml);              // Check to make sure that the type I got was what I made.             Assert.AreEqual(typeof(Schema.v2.Employee), newemp.GetType());         }          public string GetNamespace(string s)         {             XDocument z = XDocument.Parse(s);             var result = z.Root.Attributes().                     Where(a => a.IsNamespaceDeclaration).                     GroupBy(a => a.Name.Namespace == XNamespace.None ? String.Empty : a.Name.LocalName,                             a => XNamespace.Get(a.Value)).                     ToDictionary(g => g.Key,                                  g => g.First());              foreach (System.Xml.Linq.XNamespace item in result.Values)                 if (item.NamespaceName.Contains('Employee')) return item.NamespaceName;              return String.Empty;         }     }      public static class XmlSerializationExtension     {         public static string ObjectToXml<T>(this T Object)         {             XmlSerializer s = new XmlSerializer(Object.GetType());             using (StringWriter writer = new StringWriter())             {                 s.Serialize(writer, Object);                 return writer.ToString();             }         }         public static T XmlToObject<T>(this T Object, string xml)         {             XmlSerializer s = new XmlSerializer(typeof(T));             using (StringReader reader = new StringReader(xml))             {                 object obj = s.Deserialize(reader);                 return (T)obj;             }         }     }  }  namespace Schema.v1 {     [XmlRoot(ElementName = 'Employee', Namespace= 'Employee.v1', IsNullable = false)]     public class Employee     {         [XmlElement(ElementName = 'FirstName')]         public string FirstName { get; set; }         [XmlElement(ElementName = 'LastName')]         public string LastName { get; set; }     } } namespace Schema.v2 {     [XmlRoot(ElementName = 'Employee', Namespace = 'Employee.v2', IsNullable = false)]     public class Employee     {         [XmlAttribute(AttributeName = 'FirstName')]         public string FirstName { get; set; }         [XmlAttribute(AttributeName = 'LastName')]         public string LastName { get; set; }     } } 
  • 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. 2026-05-10T17:36:54+00:00Added an answer on May 10, 2026 at 5:36 pm

    Two suggestions:

    First, maybe don’t do that at all. If you’re serializing, prefer one method over the other unless the caller specifies a schema.

    Second, don’t parse the XML for discovery; just do string matching on ‘Employee.v2’ and ‘Employee.v1’ within (say) the first 100 bytes of the file (or however far you need to go to get the information). That should work, unless those are going to be common strings within your data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be an incredibly stupid question, but I have a web application that
Following on from my recent question regarding parsing XML files in Java I have
Dear g++ hackers, I have the following question. When some data of an object
I have seen the following question but I still have a few doubts. Sending
I'm sure that this is an extremely basic question but here goes anyway! I
I have the following XML that is built up at runtime using an XmlDocument
I have a huge xml file that I would like to split up into
I have an xml that has nested tags like the following: <?xml version=1.0 encoding=utf-8
I have a checkstyle.xml that looks something like this: <module name=Checker> .... <module name=SuppressionCommentFilter>
i have the following xml structure and want to add it to a treeview.

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.