Is there a way for me to serialize an object in .NET without the XML Namespaces automatically serializing also? It seems that by default .NET believes the XSI and XSD namespaces should be included, but I don't want them there.
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
If you want to get rid of the extra
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
andxmlns:xsd="http://www.w3.org/2001/XMLSchema"
, but still keep your own namespacexmlns="http://schemas.YourCompany.com/YourSchema/"
, you use the same code as above except for this simple change:If you want to remove the namespace you may also want to remove the version, to save you searching I've added that functionality so the below code will do both.
I've also wrapped it in a generic method as I'm creating very large xml files which are too large to serialize in memory so I've broken my output file down and serialize it in smaller "chunks":
If you are unable to get rid of extra xmlns attributes for each element, when serializing to xml from generated classes (e.g.: when xsd.exe was used), so you have something like:
then i would share with you what worked for me (a mix of previous answers and what i found here)
explicitly set all your different xmlns as follows:
then pass it to the serialize
you will have the three namespaces declared in the root element and no more needed to be generated in the other elements which will be prefixed accordingly
Ahh... nevermind. It's always the search after the question is posed that yields the answer. My object that is being serialized is
obj
and has already been defined. Adding an XMLSerializerNamespace with a single empty namespace to the collection does the trick.In VB like this:
in C# like this:
I Suggest this helper class:
:)