0

I see several questions that are close to this but none exactly cover it:

I can cobble something together from these but I worry I am passing it through too many steps to be efficient.

What I currently have is this, to read XML from a HTTP web request:

        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(stream);
        string xml = streamReader.ReadToEnd();

This was before the need to apply an XLST transform was needed. Now I have a (possibly null) XslCompiledTransform object.

So I want to add a block like:

if(transform != null)
{
  xml = transform.Transform(xml);
}

Clearly this isn't possible as written. I see StringReaders and XmlReaders can be created but is it inefficient to get my xml as a string and then push it back into another object? Can I use my stream or streamReader objects directly to support the same basic flow, but with optional transformation?

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

1 Answers1

0

Personally I'd use the XmlDocument.Load() function to load the XML from the URL, without using WebRequest in this case.

You can pass the XmlDocument Straight to XSLCompiledTransform.Transform() then.

XmlDocument doc = new XmlDocument();
doc.Load(url);
if (transform != null)
{
 XmlDocument tempDoc = new XmlDocument();
 using (XmlWriter writer = tempDoc.CreateNavigator().AppendChild())
 {
  transform.Transform(doc, writer);
 }
 doc = tempDoc;
} //Use your XmlDocument for your transformed output
Tim Ebenezer
  • 2,714
  • 17
  • 23
  • My issue is there mightn't be a transform and I don't want two quite different code paths. Maybe I'm just not familiar with these classes enough to see how this would hang together... could you provide sample (pseudo is fine) code? – Mr. Boy Jul 11 '16 at 13:39