question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to drop "exs:type" when serializing derived types

See original GitHub issue

This is not an issue but rather question for help.

I have following object structure:

class Container
{
    public string Common { get; set; } = "Common";
    public IBody Body { get; set; }
}

interface IBody
{
    string BodyType { get; set; }
}

class A : IBody
{
    public string BodyType { get; set; } = "A";
    public string FieldX { get; set; } = "FieldX";
}

class B : IBody
{
    public string BodyType { get; set; } = "B";
    public string FieldY { get; set; } = "FieldY";
}

I serialize it with following code:

var serializer = new ConfigurationContainer()
	.UseOptimizedNamespaces()
	.EnableImplicitTyping(typeof(Container))
	.EnableImplicitTyping(typeof(A))
	.EnableImplicitTyping(typeof(B))
	.EnableImplicitTyping(typeof(IBody))
	.Create();
var result = serializer.Serialize(new XmlWriterSettings { OmitXmlDeclaration = true}, container);

And I get this as result

<Container xmlns:exs="https://extendedxmlserializer.github.io/v2" xmlns="clr-namespace:Namespace.Test;assembly=Test"><Common>Common</Common><Body exs:type="A"><BodyType>A</BodyType><FieldX>FieldX</FieldX></Body></Container>

Is there any way to get rid of exs:type and rest of namespace related attributes somehow? I am only interested in serialization.

My receiving party is a custom XML parser and it has problems with namespace parsing…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
issue-label-bot[bot]commented, Nov 4, 2020

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.74. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

1reaction
michalsznajdercommented, Nov 5, 2020

O thanks. It would be nice to have it but I understand. Anyway - great project.

As for my case I resorted for second best thing after XSLT - regex 😃 I will leave it for reference here for future :

var cleanupRegex = new Regex("( xmlns:exs=\"[^\"]+\"| xmlns=\"[^\"]+\"| exs:type=\"[^\"]+\")");
var result = _serializer.Serialize(_writerSettings, classWithInterfaces);
result = cleanupRegex.Replace(result, "");

BTW would you accept PR that adds option to disable namespace generation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to serialize properties of derived classes with System. ...
In this article. Serialize properties of derived classes; Polymorphic type discriminators; Configure polymorphism with the contract model ...
Read more >
c# - Json.net serialize/deserialize derived types?
The issue is I have a list which contains both base and derived objects. So I need to figure out how I tell...
Read more >
Serialization of derived data types
One option is to go into details of what is actually necessary to write down for every single object. This will work but...
Read more >
Inheritance - cereal Docs
This page is concerned with how to serialize base classes from a derived class. Another important and related feature of inheritance, polymorphism, is...
Read more >
Skip serializing field
Please use the skip attribute to skip both serializing and deserializing (see Field Attributes: skip ). Likewise, use skip_deserializing to skip deserializing ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found