I am working on a project which generates an assembly. I just noticed that an additional assembly *.XmlSerializers.dll is being generated. Why this file is auto generated and what it is used for?
In .NET implementation, the XmlSerializer generates a temporary assembly for serializing/deserializing your classes (for performance reasons). It can either be generated on the fly (but it takes time on every execution), or it can be pregenerated during compilation and saved in this assembly you are asking about.
You can change this behaviour in project options (tab Compile -> Advanced Compile Options -> Generate serialization assemblies, Auto or On, respectively). The corresponding element in the project file is GenerateSerializationAssemblies, for example, <GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
.
Auto
, but only one created a serialization DLL. –
Hoisch **Generate Serialization Assemblies** Specifies whether the compiler will use the XML Serializer Generator Tool (Sgen.exe) to create XML serialization assemblies. Serialization assemblies can improve the startup performance of XmlSerializer if you have used that class to serialize types in your code. By default, this option is set to Auto, which specifies that serialization assemblies be generated only if you have used XmlSerializer to encode types in your code to XML.
msdn.microsoft.com/en-us/library/kb4wyys2.aspx –
Hoisch FYI. The exact steps to stop the XmlSerializers.dll from being auto-generated are:
- In VS, right-click your project file and select "Properties"
- Click the "Build" tab
- Change the "Generate serialization assembly" dropdown from "Auto" to "Off"
- Rebuild and it will be gone
I think this is the JIT (Just in time) compilation of XML serialisers for performance reasons.
You get the same thing with RegEx instances using the RegexOptions.Compiled option turned on.
I'm no .NET CLR expert, sorry for lack of precise technical detail.
*.XmlSerializers.dll
are generated using the Sgen.exe [XML Serializer Generator Tool]
See Sgen.exe on MSDN
Typically the Sgen.exe
is used in Post Build events of Projects. See if your project has a post build event which generates the *.XmlSerializers.dll
The project only generates the project.XMLSerialisers.dll for web applications. For other applications you have to run sgen separately.
I only have this when generating using a xmlRootAttrebute.
Not sure for your use-case but I used:
private static string ChangeRootName(string xmlString, string newRootName)
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
// Create a new root element with the specified name
XmlElement newRootElement = xmlDoc.CreateElement(newRootName);
// Move all child nodes from the old root to the new root
while (xmlDoc.DocumentElement.HasChildNodes)
{
newRootElement.AppendChild(xmlDoc.DocumentElement.FirstChild);
}
// Replace the old root with the new root
xmlDoc.ReplaceChild(newRootElement, xmlDoc.DocumentElement);
return xmlDoc.OuterXml;
}
And use the normal XmlSerialize and not have this issue.
© 2022 - 2025 — McMap. All rights reserved.