คำตอบ Transform Result Doc as XDocument

  • 27 เมษายน 2555 20:29
     
      มีโค้ด

    XPathDocument xpathDoc = new XPathDocument(sourcePath);
    XPathDocument xpathTransform = new XPathDocument(XmlReader.Create(assembly.GetManifestResourceStream("Namespace.Stylesheet.xsl")));
     XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load(xpathTransform);
    trans.Transform(xpathDoc, writer);

    How do you output an XDocument from a transform?

    The above code sends the output to an XmlTextWriter.  But, I need the output as an XDocument or, at least, an XmlDocument.

ตอบทั้งหมด

  • 27 เมษายน 2555 22:38
     
      มีโค้ด

    What is writer? XmlWriter? Then something like

    MemoryStream stream = new MemoryStream();
    XmlWriter writer = XmlWriter.Create(stream);
    
    XPathDocument xpathDoc = new XPathDocument(sourcePath);
    XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load(XmlReader.Create(assembly.GetManifestResourceStream("Namespace.Stylesheet.xsl")));
    trans.Transform(xpathDoc, writer);
    
    XDocument doc = XDocument.Load(stream);

    Or simply
    MemoryStream stream = new MemoryStream();
                
    XPathDocument xpathDoc = new XPathDocument(sourcePath);            
    XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load(XmlReader.Create(assembly.GetManifestResourceStream("Namespace.Stylesheet.xsl")));
    trans.Transform(xpathDoc, null, stream);
    
    XDocument doc = XDocument.Load(stream);

  • 28 เมษายน 2555 9:17
     
     คำตอบ มีโค้ด

    Here is a short example:

                XslCompiledTransform proc = new XslCompiledTransform();
                proc.Load("sheet.xslt");
    
                XDocument result = new XDocument();
                using (XmlWriter xw = result.CreateWriter())
                {
                    proc.Transform("input.xml", xw);
                    xw.Close();
                }
                // now use XDocument result here e.g. for testing check output
                Console.WriteLine(result);


    MVP Data Platform Development My blog

    • เสนอเป็นคำตอบโดย Martin Honnen 28 เมษายน 2555 9:17
    • ทำเครื่องหมายเป็นคำตอบโดย Newmanb1 1 พฤษภาคม 2555 15:25
    •