当我对串行数据使用XML序列化方法时,我发现错误错误2,'consoleapplication3.program.converttoxml< T>(string)'的最佳重载方法匹配有一些无效的参数 [英] When I using XML serialization method for serialsed data I found error error 2 the best overloaded method match for 'consoleapplication3.program.converttoxml<T>(string)' has some invalid arguments

查看:60
本文介绍了当我对串行数据使用XML序列化方法时,我发现错误错误2,'consoleapplication3.program.converttoxml< T>(string)'的最佳重载方法匹配有一些无效的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

;有一些无效的论点

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





我的尝试:



公共类AddressDetails

{

public int HouseNo {get;组; }

public string StreetName {get;组; }

public string City {get;组; } $ / $
私有字符串PoAddress {get;组; }

}







public T ConvertToXml< t>(字符串xml)

{

var serializer = new XmlSerializer(typeof(T));

return(T)serializer.Deserialize(new StringReader( xml));

}





static void Main(string [] args)

{





地址详细信息=新地址详细信息();

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

推荐答案

试试这个



序列化表示将对象转换为 xml ,但您已使用De-Serialize方法。

您的代码似乎正在尝试制作该功能通用的。希望它有所帮助。



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.

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


这篇关于当我对串行数据使用XML序列化方法时,我发现错误错误2,'consoleapplication3.program.converttoxml&lt; T&gt;(string)'的最佳重载方法匹配有一些无效的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆