fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml.Serialization;
  7.  
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //OutputSerialized(new Outer() { Inner = new Inner() });
  13. //OutputSerialized(new OuterCustom() { Inner = new Inner() });
  14. OutputSerialized(
  15. new Root()
  16. {
  17. Outer = new Outer() { Inner = new Inner() },
  18. OuterCustom = new OuterCustom() { Inner = new Inner() }
  19. });
  20. }
  21.  
  22. static void OutputSerialized<T>(T t)
  23. {
  24. var sb = new StringBuilder();
  25. using (var textwriter = new StringWriter(sb))
  26. new XmlSerializer(typeof(T)).Serialize(textwriter, t);
  27. Console.WriteLine(sb.ToString());
  28. }
  29. }
  30.  
  31. [Serializable]
  32. public class Root
  33. {
  34. public Outer Outer { get; set; }
  35. public OuterCustom OuterCustom { get; set; }
  36. }
  37.  
  38. [Serializable] public class Inner { }
  39.  
  40. [Serializable] public class Outer { public Inner Inner { get; set; } }
  41.  
  42. public class OuterCustom : IXmlSerializable
  43. {
  44. public Inner Inner;
  45.  
  46. public void WriteXml(System.Xml.XmlWriter writer)
  47. {
  48. writer.WriteAttributeString(
  49. "xmlns", "xsi", null, "http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance");
  50. writer.WriteAttributeString(
  51. "xmlns", "xsd", null, "http://w...content-available-to-author-only...3.org/2001/XMLSchema");
  52. writer.WriteStartElement("Inner");
  53. new XmlSerializer(typeof(Inner)).Serialize(writer, Inner);
  54. writer.WriteEndElement();
  55. }
  56.  
  57. public System.Xml.Schema.XmlSchema GetSchema() { return null; }
  58. public void ReadXml(System.Xml.XmlReader reader) { /**/ }
  59. }
  60.  
Success #stdin #stdout 0.2s 40696KB
stdin
Standard input is empty
stdout
<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:xsi="http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance" xmlns:xsd="http://w...content-available-to-author-only...3.org/2001/XMLSchema">
  <Outer>
    <Inner />
  </Outer>
  <OuterCustom xmlns:xsi="http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance" xmlns:xsd="http://w...content-available-to-author-only...3.org/2001/XMLSchema">
    <Inner>
      <Inner />
    </Inner>
  </OuterCustom>
</Root>