code_junkie

How to serialize an object to XML without getting xmlns=“…”?

Nov 12th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. Dim xs As New XmlSerializer(GetType(cEmploymentDetail))
  2. Dim ns As New XmlSerializerNamespaces()
  3. ns.Add("", "")
  4.  
  5. Dim settings As New XmlWriterSettings()
  6. settings.OmitXmlDeclaration = True
  7.  
  8. Using ms As New MemoryStream(), _
  9. sw As XmlWriter = XmlWriter.Create(ms, settings), _
  10. sr As New StreamReader(ms)
  11. xs.Serialize(sw, obj, ns)
  12. ms.Position = 0
  13. Console.WriteLine(sr.ReadToEnd())
  14. End Using
  15.  
  16. //Create our own namespaces for the output
  17. XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  18.  
  19. //Add an empty namespace and empty value
  20. ns.Add("", "");
  21.  
  22. //Create the serializer
  23. XmlSerializer slz = new XmlSerializer(someType);
  24.  
  25. //Serialize the object with our own namespaces (notice the overload)
  26. slz.Serialize(myXmlTextWriter, someObject, ns);
  27.  
  28. // Add lib namespace with empty prefix
  29. ns.Add("", "http://schemas.YourCompany.com/YourSchema/");
Add Comment
Please, Sign In to add comment