我认为XML序列化和模式推理的组合可能是您所需要的。
XmlSerializer应该足以生成您正在查找的XML文件。除了在流或流写入器中传递以及根级实体的实例外,您实际上不需要执行任何操作。 XML序列化的实际代码是2行:
// for example:
public static void Serialize<T> (T data, TextWriter writer)
{
var xs = new XmlSerializer(typeof(T))
xs.Serialize(writer, data); // write out the xml to the stream writer...
}
我不确定为什么要避免XML序列化。
为了将XSD相对于一组实体进行保存,您可以从生成的XML中推断出模式。模式推断是通过将XmlSchemaInference
类与XmlSchemaSet
结合使用完成的。
我通常创建我自己来个小架构推断效用无论我走到哪里,它看起来像这样:
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Nothing to do.");
return;
}
// first arg should contain the path to xml else error
Console.WriteLine("Inferring schema of: {0}", args[0]);
// default output should be xml name part + .xsd
InferSchema(args[0]);
}
static void InferSchema(string fileName)
{
XmlWriter writer = null;
XmlSchemaInference infer = new XmlSchemaInference();
XmlSchemaSet sc = new XmlSchemaSet();
string outputXsd = fileName.Replace(".xml", ".xsd");
sc = infer.InferSchema(new XmlTextReader(fileName));
using (writer =
XmlWriter.Create(new StreamWriter(outputXsd)))
{
foreach(XmlSchema schema in sc.Schemas())
{
schema.Write(writer);
Console.WriteLine(">> found schema - generated to {0}",
outputXsd);
}
}
}
}
它没有什么比这再简单不过。
Thx为快速回答。重新序列化 - 不知道为什么我要避免它,说实话。我有一个印象,序列化文件会给我更多关于类的信息,而不仅仅是结构或数据。在重新考虑它时,你可能是对的。需要咖啡。至于XSD - 我一定会尝试一下。看起来像这可能工作。 Thx一堆! – 2010-10-15 20:27:51
好吧,我开始测试这个,并且遇到了奇怪的结果。我得到了序列化的工作,但获得的XML文件只不过是描述实体中主表的头标签。没有数据。我确保懒惰加载关闭,甚至“监视”检索到的对象,以确保它有数据,但没有运气。我会继续挖掘。有任何想法吗 ? – 2010-10-18 13:32:50
次要更新 - 序列化程序没有太大的作用,因为我定义的对象类型是var类型而不是实际的EntityType。我现在的问题是,只有来自顶级EntityType的数据才被序列化;即使我在Linq查询中使用.Include()引用它们的导航属性,我也从子表中得不到任何东西。 Wewy wewy stwange。 – 2010-10-18 13:44:54