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

不羁的心 提交于 2020-01-12 15:52:36

问题


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);

回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/12201627/whats-the-non-deprecated-alternative-to-xmldatadocument-and-xsltransform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!