CallingC# List <> để xml
List<PC> _PCList = new List<PC>();
...add Pc to PCList..
WriteXML<List<PC>>(_PCList, "ss.xml");
Chức năng
public static void WriteXML<T>(T o, string filename)
{
string filePath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Genweb2\\ADSnopper\\" + filename;
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("TheRootElementName"));
ser.Serialize(writer, o); // error
}
File.WriteAllText(filePath,xmlDoc.InnerXml);
}
ngoại lệ nội
Không thể cast đối tượng của loại 'System.Collections.Generic.List
1[PC]' to type 'System.Collections.Generic.List
1 [ System.Collections.Generic.List`1 [PC]] '.
Xin vui lòng giúp
Đừng quên upvote và đánh dấu nó như được chấp nhận nếu nó làm việc cho bạn –