2
我可能在这里做的这一切都是错误的,但我创建了一个包含列表的类,并且我需要序列化该列表。 (有没有更好的方法或其他建议有多个接口没有列表?)序列化一个包含列表的类?
我一直在做序列化自定义类,但这个由于某种原因没有工作。
[XmlRoot("interfaces", Namespace = "")]
[XmlInclude(typeof(Interface))]
public class Interfaces
{
[XmlArray("interfaces")]
[XmlArrayItem("interface")]
List<Interface> _IflList = new List<Interface>();
public List<Interface> IflList
{
get { return _IflList; }
set { _IflList = value; }
}
public void Add(Interface objInterface)
{
_IflList.Add(objInterface);
}
}
[XmlType("interface")]
public class Interface
{
string _name;
public string name
{
get { return _name; }
set { _name = value; }
}
public Interface(string name)
{
this._name = name;
}
}
我试图序列与时收到错误There was an error reflecting type 'JunOSConfig.Interfaces'
:
public string SerializeObject(object objToSerialize, bool StripXmlVer, bool FormatOutput)
{
string strReturn = "";
XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
xns.Add("", "");
Type objType = typeof(JunOSConfig.Interfaces);
XmlSerializer xs = new XmlSerializer(objToSerialize.GetType());
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = StripXmlVer;
StringWriter sw = new StringWriter();
XmlWriter xw = XmlWriter.Create(sw, xws);
xs.Serialize(xw, objToSerialize, xns);
strReturn = sw.ToString();
if (FormatOutput)
{
return Convert.ToString(XElement.Parse(strReturn));
}
else
{
return strReturn;
}
}
您将需要一个无参数构造函数 –
一般来说,当您发布异常时,您还应该发布所有内部异常(使用ex.ToString())。对于XML序列化程序的异常尤其如此。内部例外很可能会告诉你到底发生了什么问题以及如何解决问题。 –
是不是Imterface受保护的关键字? – Bit