Skip to content
← All posts
· Delta1 Labs Nebula.NETObfuscationXmlSerializer.NET

Why renaming breaks XmlSerializer output

XmlSerializer has no element-name string literals in your assembly for an obfuscator to see, yet renaming silently rewrites your XML. The names come from a serialization assembly generated at runtime by reflecting over your member names — so rename a property and the wire element renames with it, breaking every stored document and every interop contract. Here is the mechanism, and how [XmlElement] pins the wire format so the rest of the type can rename freely.

XmlSerializer is the quiet exception to almost every rule of thumb about obfuscation. The usual advice is that string literals are the leak: turn on string encryption and the names in your code stop sitting in cleartext. But open an assembly that serializes with XmlSerializer, encrypt every string, and the XML it writes will still change the moment you rename the type — even though there is not a single element-name literal anywhere in your compiled IL for the obfuscator to have touched.

That is the surprising part. The element names your XML uses are nowhere in the assembly you ship. They are produced, at runtime, from something renaming does reach. This post is about where those names actually come from, why string encryption and control-flow obfuscation are irrelevant to them, and how to pin the wire format so you can rename everything else.

Where the element names come from

Start with an ordinary serializable type and no attributes:

public class Customer
{
    public string FullName { get; set; }
    public string Email { get; set; }
    public int LoyaltyPoints { get; set; }
}

Serialize one instance:

var serializer = new XmlSerializer(typeof(Customer));
using var writer = new StringWriter();
serializer.Serialize(writer, new Customer
{
    FullName = "Ada Lovelace",
    Email = "ada@example.com",
    LoyaltyPoints = 42
});

You get the expected document, with element names that match your member names:

<?xml version="1.0" encoding="utf-16"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FullName>Ada Lovelace</FullName>
  <Email>ada@example.com</Email>
  <LoyaltyPoints>42</LoyaltyPoints>
</Customer>

The root element is the type name, Customer. Each child element is a member name, FullName, Email, LoyaltyPoints. Nothing in your code wrote those strings; XmlSerializer derived them. Here is how.

When you call new XmlSerializer(typeof(Customer)), the serializer does not walk your object with reflection on every call — that would be far too slow. Instead it generates a serialization assembly at runtime. It reflects over the type’s public, read-write properties and fields, emits C# for a pair of reader/writer classes into a temporary assembly, compiles it, and caches it. Every subsequent serialize and deserialize runs that generated code.

Decompile the generated writer and the element names are suddenly visible as literals — but these literals live in the generated assembly, not in yours:

// Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1
void Write3_Customer(string n, string ns, Customer o, bool isNullable, bool needType)
{
    if (o == null) { /* ... */ return; }
    WriteStartElement(n, ns, o, false, null);
    WriteElementString(@"FullName", @"", o.@FullName);
    WriteElementString(@"Email", @"", o.@Email);
    WriteElementStringRaw(@"LoyaltyPoints", @"", XmlConvert.ToString(o.@LoyaltyPoints));
    WriteEndElement(o);
}

The string @"FullName" was produced by reading PropertyInfo.Name at the moment new XmlSerializer(...) ran. That is the whole mechanism, and it is the reason obfuscation behaves so strangely here: the wire name is a function of the member name in metadata, evaluated at runtime.

Why renaming moves the wire format

Now obfuscate the assembly with renaming on. Customer becomes a; FullName, Email and LoyaltyPoints become b, c, d in the metadata. Your logic is unchanged — the obfuscated program still populates the same fields and still calls XmlSerializer. But when it constructs the serializer, reflection now reports the members as b, c, d, so the generated writer becomes:

void Write3_a(string n, string ns, a o, bool isNullable, bool needType)
{
    WriteStartElement(n, ns, o, false, null);
    WriteElementString(@"b", @"", o.@b);
    WriteElementString(@"c", @"", o.@c);
    WriteElementStringRaw(@"d", @"", XmlConvert.ToString(o.@d));
    WriteEndElement(o);
}

And the document it writes is:

<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="...">
  <b>Ada Lovelace</b>
  <c>ada@example.com</c>
  <d>42</d>
</a>

Every stored Customer document written by the previous release now fails to deserialize — the deserializer, generated from the same renamed metadata, is looking for <a>/<b>/<c>/<d> and finds <Customer>/<FullName>/…. Any other system that consumed your XML breaks on the schema change. And none of this surfaces at compile time; it appears the first time the serialization path runs against real data.

Without attributes — the element name tracks the member namemetadatamember: FullNameXmlSerializerreflect at runtimegenerated writerWriteElementString("FullName")wire<FullName>Ada</FullName>After renaming FullName to b — the wire silently changesmetadatamember: bXmlSerializerreflect at runtimegenerated writerWriteElementString("b")wire — broken<b>Ada</b>With [XmlElement("fullName")] — the literal pins the wire namemetadatamember: b + attr blobXmlSerializerread attribute firstgenerated writerWriteElementString("fullName")wire — stable<fullName>Ada</fullName>
XmlSerializer derives element names from member names at runtime, so renaming moves the wire format. An explicit [XmlElement] name pins it.

This is also why the other obfuscation transforms are beside the point here. String encryption rewrites the ldstr operands in your IL — but there is no element-name ldstr in your IL; the literal exists only in the generated assembly, created after your program starts, which the obfuscator never sees. Control-flow obfuscation reshapes your method bodies — but the body that writes the element name is generated, not yours. The one transform that reaches the names is renaming, and it reaches them indirectly, through the metadata that reflection reports.

Pin the wire format with attributes

The fix is not to stop renaming the type. It is to stop the wire name from depending on the member name at all. Every XmlSerializer naming decision can be overridden by an attribute that carries a literal:

[XmlRoot("customer")]
public class Customer
{
    [XmlElement("fullName")]
    public string FullName { get; set; }

    [XmlElement("email")]
    public string Email { get; set; }

    [XmlElement("loyaltyPoints")]
    public int LoyaltyPoints { get; set; }
}

When XmlSerializer builds the generated writer, it reads the attribute’s literal before it falls back to the member name. So the generated code emits WriteElementString(@"fullName", …) whether the property is called FullName or b. The decisive detail for obfuscation: that literal is stored in the type’s custom-attribute blob in metadata, and Nebula does not rewrite attribute-blob strings. Renaming moves FullName to b; the "fullName" in the attribute blob is left exactly as written. The wire format is now independent of the C# name, so the member renames freely and the XML never changes.

The full attribute set covers every shape the serializer names by default: [XmlRoot] and [XmlType] for the element and schema-type names, [XmlElement] and [XmlAttribute] for members, [XmlArray] / [XmlArrayItem] for collections, and [XmlEnum] for enum members (whose value text is otherwise the renamed field name). Polymorphic graphs need particular care: [XmlInclude(typeof(PremiumCustomer))] lets the base serializer write a derived instance, and the derived type is tagged in the XML with xsi:type="PremiumCustomer" — the type name again. Give every included type an [XmlType("premiumCustomer")] so that discriminator is a literal too.

In Nebula terms, anchoring is the right tool because it is surgical. You are not carving a readable hole in the assembly — the type still renames, and its method bodies are still control-flow-obfuscated and its own string literals still encrypted. You have only pinned the handful of strings that are a contract. Your nebula.config.json needs no special rule for these types:

{
  "schemaVersion": 1,
  "inputs": [ "bin/Release/net8.0/Acme.Billing.dll" ],
  "outputDirectory": "protected",
  "preservePublicApi": false,
  "encryptStrings": true,
  "controlFlowObfuscation": true,
  "renaming": {
    "rules": [
      { "pattern": "Acme.Billing.*", "action": "rename" }
    ]
  }
}

If a type genuinely cannot be annotated — a model you do not own, or one generated by a tool — fall back to a keep rule for that type only ({ "pattern": "Acme.Billing.ExternalModels.*", "action": "keep", "members": "public" }), knowing those names stay in cleartext. The companion piece on choosing what to exclude from renaming covers how to keep that fallback narrow.

When it still breaks

A few sharp edges remain even after you anchor.

Precompiled serializers. If you precompile the serializer into a sidecar Acme.Billing.XmlSerializers.dll (via sgen or PublishSerializers), that assembly already froze the names at build time. Obfuscate the main assembly afterward and the two diverge — the serializer emits references to members that no longer exist under those names, and it fails to load. Anchoring with attributes also fixes this, because the frozen names are the pinned literals; otherwise you must regenerate the serializers assembly from the obfuscated output.

IXmlSerializable. Types that implement IXmlSerializable write their own XML in WriteXml, so the element names are ldstr literals in your IL — which means string encryption now does apply, and encrypting a name you use as an element name will break the wire format. Keep those names as plaintext literals or route them through the same attribute-style constants.

Case and exact match. XML is case-sensitive and the serializer matches element names exactly. A pinned "fullName" and a stored "FullName" are different elements; decide the casing once and keep it, because an obfuscation pass is exactly the kind of release where a silent rename would otherwise slip in.

Takeaway

XmlSerializer writes its element names from your member and type names, evaluated by reflection at runtime inside a generated serialization assembly. That assembly is invisible to your obfuscator, so string encryption and control-flow obfuscation never touch the names — but renaming moves the metadata those names are read from, and the wire format moves with it. Pin every serialized name with an explicit [XmlRoot], [XmlElement], [XmlAttribute], [XmlType] or [XmlEnum], and the contract stops depending on the C# name at all: Nebula.NET renames the type to a and encrypts its internals while the XML stays byte-for-byte stable. Then prove it — round-trip a real document against the protected build, and open the result in Glass.NET to confirm the type reads as a while the pinned names survive.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.