XML serialization with namespace setting : Xml serialization : XML C# Examples


C# Examples » XML » Xml serialization »

 

XML serialization with namespace setting









    
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;
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» Xml serialization