XmlSerializer.Serialize : XmlSerializer « System.Xml.Serialization « C# / C Sharp by API






XmlSerializer.Serialize

  

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

class MainClass{

  public static void Main(){
     try {
         ShapedRoot sr = new ShapedRoot();
         sr.node = new Shaped();
         sr.node.s1 = "a value";
         sr.node.s2 = "another value";
         sr.node.s3 = "uuid:1AE0964A-2B30-4a02-96F2-325B92B4E92C";

         StringWriter sw = new StringWriter();
         XmlTextWriter tw = new XmlTextWriter( sw );
         tw.Formatting = Formatting.Indented;
         tw.Indentation = 4;

         XmlSerializer ser = new XmlSerializer( typeof( ShapedRoot ) );
         ser.Serialize( tw, sr );

         tw.Close();
         sw.Close();

     }
     catch( Exception exc )
     {
         Console.WriteLine( exc.Message );
     }
 
  }
}

[XmlRoot( ElementName="sroot", Namespace="urn:my-examples:shaping" )]
public class ShapedRoot
{
    public ShapedRoot()
    {
    }

    public Shaped node;
}

[XmlType( Namespace="urn:my-examples:shaping" )]
public class Shaped
{
    public Shaped()
    {
    }

    [XmlAttribute]
    public string s1;

    [XmlElement( ElementName = "string2" )]
    public string s2;

    [XmlElement( DataType = "anyURI" )]
    public string s3;
}

   
    
  








Related examples in the same category

1.new XmlSerializer
2.XmlSerializer.Deserialize