locked
Serialization RRS feed

  • Question

  • Hi

    I need to serialize a object from c# into xml through powershell. I want to use the xml attributes in my c# class which also have some prefix and namespaces. How can i do that ?

    C# class below

    using System;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    using System.Xml;
    namespace MyNamespace
    {
     public class Test
        {
            [SoapElement("user_name")]
            public string EmployeeId { get; set; }
            [SoapElement("email")]
            public string PrivateEmail { get; set; }
        }
    }

    Tuesday, March 6, 2018 8:47 AM

Answers

  • PS D:\scripts> $code = @'
    >> using System;
    >> using System.Collections.Generic;
    >> using System.Xml.Serialization;
    >> using System.Xml;
    >>
    >> namespace MyNamespace{
    >>     public class Test{
    >>         [SoapElement("user_name")]
    >>         public string EmployeeId { get; set; }
    >>         [SoapElement("email")]
    >>         public string PrivateEmail { get; set; }
    >>     }
    >> }
    >> '@
    PS D:\scripts> Add-Type $code -ReferencedAssemblies System.Xml
    PS D:\scripts> $Test = [MyNamespace.Test]::New()
    PS D:\scripts> $test.EmployeeId = 12345
    PS D:\scripts> $test.PrivateEmail = 'joe@mars.net'
    PS D:\scripts> $test
    
    EmployeeId PrivateEmail
    ---------- ------------
    12345      joe@mars.net
    
    
    PS D:\scripts>
    
    
    


    \_(ツ)_/

    Tuesday, March 6, 2018 9:15 AM