Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
; has some invalid arguments
C#
public class AddressDetails
    {
        public int HouseNo { get; set; }
        public string StreetName { get; set; }
        public string City { get; set; }
        private string PoAddress { get; set; }
    }



    public T ConvertToXml<T>(string xml)
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(new StringReader(xml));
    }


     static void Main(string[] args)
     {


         AddressDetails details = new AddressDetails();
         details.HouseNo = 4;
         details.StreetName = "Rohini";
         details.City = "Delhi";
         ConvertToXml<string>(details);
     }


What I have tried:

public class AddressDetails
{
public int HouseNo { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
private string PoAddress { get; set; }
}



public T ConvertToXml<t>(string xml)
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(new StringReader(xml));
}


static void Main(string[] args)
{


AddressDetails details = new AddressDetails();
details.HouseNo = 4;
details.StreetName = "Rohini";
details.City = "Delhi";
ConvertToXml<string>(details);
}
Posted
Updated 26-May-16 2:09am
v2
Comments
Richard MacCutchan 26-May-16 7:42am    
What error? Where did you find it? Please edit your question and add full details.
George Jonsson 26-May-16 7:45am    
And the error is?

1 solution

Try this

Serialization means converting the object to an xml but you have used De-Serialize method.
your code seems that you are try to make the function generic. hope it helps.

C#
using System;
using System.Xml.Serialization;



public class AddressDetails
{
    public int HouseNo { get; set; }
    public string StreetName { get; set; }
    public string City { get; set; }
    private string PoAddress { get; set; }
}

public class MyCLass
{

    public static string ConvertToXml<T>(object xml)
    {

        var stringwriter = new System.IO.StringWriter();
        var serializer = new XmlSerializer(typeof(T));
        serializer.Serialize(stringwriter, xml);
        return stringwriter.ToString();
    } 

    static void Main(string[] args)
    {


        AddressDetails details = new AddressDetails();
        details.HouseNo = 4;
        details.StreetName = "Rohini";
        details.City = "Delhi";
        var xmlString = ConvertToXml<AddressDetails>(details);
        Console.WriteLine(xmlString);
        Console.ReadLine();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900