找到了一种方法来做到这一点。它有它的障碍,但是一旦你把所有东西都包括在内,它就会非常顺利地工作。
要生成 XML:
DataContractSerializer ser = new DataContractSerializer(typeof(Project));
StringBuilder objectSB = new StringBuilder();
XmlWriter objectWriter = XmlWriter.Create(objectSB);
//I'm using this method in the object I want to serialize (hence the 'this')
ser.WriteObject(objectWriter, this);
objectWriter.Flush();
然后,您可以将 StringBuilder 转换为 Linq to XML 的 XElement:
Xelement xProject = Xelement.Parse(objectSB.ToString());
对于反序列化:
DataContractSerializer ser = new DataContractSerializer(typeof(Project));
Project project = (CoilProject)ser.ReadObject(xProject.CreateReader());
有几次我使用 Linq to XML 编辑 XML 而不进行反序列化和重新序列化。由于 DataContractSerializer 添加的命名空间,这变得很有必要:
//place this somewhere in your project's namespace
public static class MyExtensions
{
public static IEnumerable<XElement> ElementsAnyNS(this XElement source, string localName)
{
return source.Elements().Where(e => e.Name.LocalName == localName);
}
public static XElement ElementAnyNS(this XElement source, string localName)
{
return source.Elements().Where(e => e.Name.LocalName == localName).Select(e => e).Single();
}
}
这些扩展方法允许您从序列化对象中选择一个或多个元素,而无需担心命名空间。我从Pavel Minaev对Ignore namespaces in LINQ to XML的回答中改编了这些扩展方法。