Hi, I am new to this Forum and I hope my question is not out of scope. The following 4 lines of C# code allow me output specific fields from my XML to an HTML file (result.html). The code works fine when I have an XML file (sample1.xml) and an XSLT file (sample1.xslt) as an input. Now I want to modify this code to be able to input a string (the same XML code) instead of an actual XML file.

XPathDocument myXPathDoc = new XPathDocument("sample1.xslt");
            
XslTransform myXslTrans = new XslTransform();

myXslTrans.Load("sample1.xslt");

XmlTextWriter myWriter = new XmlTextWriter("result.html", null);

myXslTrans.Transform(myXPathDoc, null, myWriter);

I am looking for a way to stream a string to that XPathDocument() or any other solution is welcome...
Thanks

Recommended Answers

All 3 Replies

Hello etm9413 and welcome to daniweb! :)

Please use code tags when you paste your code:

[code=c#] ...code here

[/code]

As far as your question the subject is a leading misleading as this is not streaming XML. Here is how you can convert a string to a stream. I'm not familiar with the classes you are using so I can't give you a complete code sample:

string sampleXMLData = @"<xml><sample>a</sample></xml>";
      using (MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(sampleXMLData)))
      {
        ms.Position = 0;
        ms.Seek(0, SeekOrigin.Begin);
        XPathDocument xmlDoc = new XPathDocument(ms);
      }

Hi,
Simply user XmlDocument and its method LoadXml().
You can find example code here.

Ok Thanks sknake,
your code works fine for me and I will make sure to post my code in an appropriate manner next time. Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.