C# 从对象到XPathDocument的最快方式

C# 从对象到XPathDocument的最快方式,c#,serialization,C#,Serialization,这个问题我在过去遇到过好几次。我的解决方案总是显得过于复杂。如何以最少的步骤从一个对象转到一个XPathDocument?在过去,我创建了一个MemoryStream,但是这个解决方案总是需要大量的处理,这会导致糟糕的代码 你有什么建议 static void Main(string[] args) { itemOrder order = GenerateTestItem(); XPathDocument

这个问题我在过去遇到过好几次。我的解决方案总是显得过于复杂。如何以最少的步骤从一个对象转到一个
XPathDocument
?在过去,我创建了一个
MemoryStream
,但是这个解决方案总是需要大量的处理,这会导致糟糕的代码

你有什么建议

 static void Main(string[] args)
        {
            itemOrder order = GenerateTestItem();            

            XPathDocument doc = new XPathDocument(order);//wish it took the object directly...
            XslTransform transform = new XslTransform();
            transform.Load("Test.xslt");
            XmlTextWriter writer = new XmlTextWriter("result.html",null);
            transform.Transform(doc, null, writer);
            Console.Write(writer);
            writer.Close();
        }

您不能使用XPathDocument,因为它需要Xml文档作为存储


您可以实现底层接口IXPathNavigatable和相应的XPathNavigator类来遍历对象。以下文章介绍了这种方法:。XslTransform有相应的转换方法,您可以在以后应用Xsl:

您不能使用XPathDocument,因为它需要Xml文档作为存储


您可以实现底层接口IXPathNavigatable和相应的XPathNavigator类来遍历对象。以下文章介绍了这种方法:。XslTransform有相应的转换方法,您可以在以后应用Xsl:

您只想进行转换吗?您可以编写一个助手,function;)@Kees-对于这个过程,是的。在不久的将来,我可能需要阅读xml文档。您只想进行转换吗?您可以编写一个助手,function;)@Kees-对于这个过程,是的。在不久的将来,我可能需要阅读xml文档。