Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the non-deprecated alternative to XmlDataDocument and XslTransform?

Tags:

c#

.net-4.0

xslt

I am modifying some legacy code to try to eliminate warnings. XmlDataDocument and XslTransform both generate warnings that they are obsolete. In the case of XslTransform the suggested replacement is XslCompiledTransform, but no replacement is suggested for XmlDataDocument.

How can I change this code to eliminate warnings in .NET 4:

var xmlDoc = new System.Xml.XmlDataDocument(myDataSet);
var xslTran = new System.Xml.Xsl.XslTransform();
xslTran.Load(new XmlTextReader(myMemoryStream), null, null);
var sw = new System.IO.StringWriter();
xslTran.Transform(xmlDoc, null, sw, null);
like image 712
Alexis Reyes Avatar asked Aug 30 '12 16:08

Alexis Reyes


2 Answers

XDocument doc = new XDocument();
using (XmlWriter xw = doc.CreateWriter())
{
  myDataSet.WriteXml(xw);
  xw.Close();
}

XslCompiledTransform proc = new XslCompiledTransform();
using (XmlReader xr = XmlReader.Create(myMemoryStream))
{
  proc.Load(xr);
}

string result;

using (StringWriter sw = new StringWriter())
{
  proc.Transform(doc.CreateNavigator(), null, sw);  // needs using System.Xml.XPath;
  result = sw.ToString();
}

should do I think. Of course I have only used that MemoryStream for loading the stylesheet and the StringWriter for sending the transformation result to as you code snippet used those. Usually there are other input sources or output destinations like files, or streams or Textreader.

like image 123
Martin Honnen Avatar answered Nov 04 '22 04:11

Martin Honnen


XMLDocument is really your main option. I'm not 100% sure what you're trying to do with the code block you've posted, but you can give something like this a shot:

public void DoThingsWithXml() 
{
  string strXdoc = src.GetTheXmlString(); // however it is you do it
  XmlDocument xdoc = new XmlDocument();
  xdoc.LoadXml(strXdoc);
  // The other things you need to do
}
like image 23
tmesser Avatar answered Nov 04 '22 04:11

tmesser