我想使用Linq保存并使用XML加载此类:
public class AssemblyVerwaltung
{
public string AssemblyName;
public List<string> List_KlassenNamen;
}
在Form1中,我有一个这个类的列表:
List<AssemblyVerwaltung> List_AssemblyVerwaltung;
Save mehtode看起来像那样
void Save(XMLPath)
{
//need help hear
//Save List of "AssemblyVerwaltung" with Linq to XML
}
并且Load Mehtode看起来像那样
void LoadXML(XMLPath)
{
List_AssemblyVerwaltung = //need help hear (load XML with Linq)
}
答案 0 :(得分:0)
您只能在XML中拥有一个根节点。所以你必须使用根元素包装代码。请尝试以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication34
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
List<AssemblyVerwaltung> List_AssemblyVerwaltung = new List<AssemblyVerwaltung>();
Root root = new Root();
root.List_AssemblyVerwaltung = new List<AssemblyVerwaltung>(List_AssemblyVerwaltung);
XmlSerializer serializer = new XmlSerializer(typeof(Root));
StreamWriter writer = new StreamWriter(FILENAME);
XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
_ns.Add("", "");
serializer.Serialize(writer, root, _ns);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Root));
XmlTextReader reader = new XmlTextReader(FILENAME);
Root newRoot = (Root)xs.Deserialize(reader);
}
}
[XmlRoot("Root")]
public class Root
{
[XmlElement("List_AssemblyVerwaltung")]
public List<AssemblyVerwaltung> List_AssemblyVerwaltung { get; set; }
}
[XmlRoot("List_AssemblyVerwaltung")]
public class AssemblyVerwaltung
{
[XmlElement("AssemblyName")]
public string AssemblyName {get;set;}
[XmlElement("List_KlassenNamen")]
public List<string> List_KlassenNamen {get;set;}
}
}