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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:45:34+00:00 2026-06-13T13:45:34+00:00

I’ve got this code: [XmlType( Metadata )] [Serializable] public class MetadataContainer : List<MetadataBase> {

  • 0

I’ve got this code:

[XmlType( "Metadata" )]
[Serializable]
public class MetadataContainer : List<MetadataBase>
{
}

[XmlType( "Meta" )]
[XmlInclude( typeof( ReadonlyMetadata ) )]
[Serializable]
public abstract class MetadataBase
{
}

[XmlType( "Readonly" )]
[Serializable]
public class ReadonlyMetadata : MetadataBase
{
}

[TestFixture]
public class SerializationTests
{
    [Test]
    public void Can_deserialize_with_known_type()
    {
        const string text = @"<Metadata xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
                        <Meta xsi:type=""Readonly"" />
                    </Metadata>";

        var serializer = new XmlSerializer( typeof( MetadataContainer ) );
        var metas = (MetadataContainer)serializer.Deserialize( XmlReader.Create( new StringReader( text ) ) );

        Assert.That( metas.Count, Is.EqualTo( 1 ) );
        Assert.That( metas.First(), Is.InstanceOf<ReadonlyMetadata>() );
    }

    [Test]
    public void Can_deserialize_with_unknown_type()
    {
        const string text = @"<Metadata xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
                        <Meta xsi:type=""Hello"" />
                    </Metadata>";

        var serializer = new XmlSerializer( typeof( MetadataContainer ) );
        var metas = (MetadataContainer)serializer.Deserialize( XmlReader.Create( new StringReader( text ) ) );

        Assert.That( metas.Count, Is.EqualTo( 0 ) );
    }
}

The first test works, but when I run the second I get this error:

System.InvalidOperationException : There is an error in XML document (2, 9).
—-> System.InvalidOperationException : The specified type was not recognized: name=’Hello’, namespace=”, at .

Instead of getting this error I would like it to ignore not recognized types. Is there any way to do this?

  • 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-13T13:45:35+00:00Added an answer on June 13, 2026 at 1:45 pm

    Generic solution for similar problems:

    Have a look at unknown element event (link) and unknown attribute event (link) and see if they solve the problems, or we have to get dirty. Read on…

    Working solution for this problem

    Bear in mind that that I have no idea what your task is, AFAIK it is serializing xml into your datastructure. If you can change the datastructure I would recommend you to have a look at Linq2XML and create a smart factory for your purposes.

    [TestMethod]
    public void TestLinq2Xml()
    {
      const string text = @"<Metadata xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
                                <Meta xsi:type=""Readonly"" />
                                <Meta xsi:type=""Garbage"" />
                          </Metadata>";
    
      // Get the "names" of all implementors of MetadataBase
      var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
         .SelectMany(s => s.GetTypes())
             .Where(p => typeof(MetadataBase).IsAssignableFrom(p) && !p.IsAbstract && !p.IsInterface)
             .Where(t => t.GetCustomAttributes(typeof(XmlTypeAttribute), false).Any())
             .Select(t => t.GetCustomAttributes(typeof(XmlTypeAttribute), false)
                 .Cast<XmlTypeAttribute>().First().TypeName);
    
      // Create a parser
      var parser = new XmlSerializer(typeof(MetadataBase));
    
      // Create metadatacontainer to fill
      var metas = new MetadataContainer();
      // Fill it with matching from from the XML
      metas.AddRange((from t in XDocument.Parse(text).Descendants("Meta")
                    where types.Contains(t.Attribute(XName.Get("type", "http://www.w3.org/2001/XMLSchema-instance")).Value)
                    select (MetadataBase)parser.Deserialize(t.CreateReader())).ToList());
    
      // Should be one guy present
      Assert.AreEqual(metas.Count, 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
This could be a duplicate question, but I have no idea what search terms

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.