I have to transform .xml two times with different .xslt files and I know how to transform it once, but don't have idea, how to repeat it, because when I my output was something like html string and i changed my code, by using XmlWriter instead of StringWriter (commented), but it generates an empty Xml so it can't be transferred again.
public static HtmlString RenderXml(this HtmlHelper helper, string xml, string transformXsltPath, string xsltPath)
{
xml = System.IO.File.ReadAllText(("C:/Users/Student/Documents/Visual Studio 2010/Projects/MvcApplication2/MvcApplication2/schemat.xsd"));
XsltArgumentList args = new XsltArgumentList();
XslCompiledTransform t = new XslCompiledTransform();
t.Load(transformXsltPath);
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;;
using (XmlReader reader = XmlReader.Create(new StringReader(xml), settings))
{
//StringWriter writer = new StringWriter();
XmlWriter writer = XmlWriter.Create(new StringWriter());
t.Transform(reader, args, writer);
XslCompiledTransform t2 = new XslCompiledTransform();
t2.Load(xsltPath);
XmlReader reader2 = XmlReader.Create(new StringReader(writer.ToString()), settings);
StringWriter writer2 = new StringWriter();
t2.Transform(reader2, args, writer2);
HtmlString htmlString = new HtmlString(writer2.ToString());
return htmlString;
}
}
Related
I have a project scanner complaining with a Warning about
XmlDataDocument serializedContent = new XmlDataDocument();
and
serializedContent.Load(objStream);
and giving this recommendation to use:
The best way to prevent XXE attacks is to disable XML entity
resolution by disabling inline DD setting DtdProcessing to
DtdProcessing.Prohibit or by disabling XML Entity resolution setting
the XmlReaderSettings.XmlResolver property to null:
XmlReaderSettings settings = new XmlReaderSettings () ;
settings.DtdProcessing = DtdProcessing. Prohibit;
settings.XmlResolver = null;
XmlReader reader = XmlReader.Create(stream, settings);
Here's the code that I have:
[Serializable]
...
XmlTextWriter objSerializer = new XmlSerializer(...);
MemoryStream objStream = new MemoryStream();
XmlTextWriter objwriter = null;
XmlDataDocument objSerializedContent = new XmlDataDocument(); // it complains here
objWriter = new XmlTextWriter (objStream, System.Text.Encoding.UTF8);
...
objSerializedContent.Load(objStream); // it complains here
How can I apply that scan recommendation using reader if I'm using XmlTextWriter & XmlDataDocument?
If you use
XmlReaderSettings settings = new XmlReaderSettings () ;
settings.DtdProcessing = DtdProcessing. Prohibit;
settings.XmlResolver = null;
and
using (XmlReader xr = XmlReader.Create(objStream, settings)) {
objSerializedContent.Load(xr);
}
instead of objSerializedContent.Load(objStream) your code uses an XmlReader over the MemoryStream where the XmlReader has the settings you want or need.
I have some xml data that looks like..
<Root>
<Data>Nack</Data>
<Data>Nelly</Data>
</Root>
I want to add "<?xml version=\"1.0\"?>" to this string. Then preserve the xml as a string.
I attempted a few things..
This breaks and loses the original xml string
myOriginalXml="<?xml version=\"1.0\"?>" + myOriginalXml;
This doesnt do anything, just keeps the original xml data with no declaration attached.
XmlDocument doc = new XmlDocument();
doc.LoadXml(myOriginalXml);
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8","no");
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
string xmlString = sw.ToString();
This is also not seeming to have any effect..
XmlDocument doc = new XmlDocument();
doc.LoadXml(myOriginalXml);
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "no");
MemoryStream xmlStream = new MemoryStream();
doc.Save(xmlStream);
xmlStream.Flush();
xmlStream.Position = 0;
doc.Load(xmlStream);
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
string xmlString = sw.ToString();
Use an xmlwritersettings to achieve greater control over saving. The XmlWriter.Create accepts that setting (instead of the default contructor)
var myOriginalXml = #"<Root>
<Data>Nack</Data>
<Data>Nelly</Data>
</Root>";
var doc = new XmlDocument();
doc.LoadXml(myOriginalXml);
var ms = new MemoryStream();
var tx = XmlWriter.Create(ms,
new XmlWriterSettings {
OmitXmlDeclaration = false,
ConformanceLevel= ConformanceLevel.Document,
Encoding = UTF8Encoding.UTF8 });
doc.Save(tx);
var xmlString = UTF8Encoding.UTF8.GetString(ms.ToArray());
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);
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.
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
}
I am trying to use a XslCompiledTransform, and use the output as a XPathDocument.
Any Ideas?
Mr. Jones's answer was very helpful for me, but I found that the last line didn't work. I ended up doing this:
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(filePath);
StringWriter stringWriter = new StringWriter();
XmlWriter xmlWriter = XmlTextWriter.Create(stringWriter);
xsl.Transform(xPathDoc, xmlWriter);
String newXml = stringWriter.ToString();
StringReader stringReader = new StringReader(newXml);
xPathDoc = new XPathDocument(stringReader);
(Here, xPathDoc is an XPathDocument that has already been initialized from an XmlReader.)
Send the transform to an XmlTextWriter based on a StringWriter. Then instance the XPathDocument by retreiving the XML string from the StringWriter.
var sw = new StringWriter();
var xtw = new XmlTextWriter(sw);
myTransform.Transform(myXml, xtw);
var xpd = new XPathDocument(sw.ToString());
Its not the most memory efficient mechanism but will be adequate for most needs. A similar approach would be use a MemoryStream instead of a StringWriter but its a little messy by comparison.
A slightly better form of David M. Anderson's answer is below: it does not suffer from potential resource leaks; otherwise it is the same.
private static XPathDocument TransformToXPathDocument(string styleSheetPath,
IXPathNavigable xPathDoc)
{
var xsl = new XslCompiledTransform();
xsl.Load(styleSheetPath);
using(var stringWriter = new StringWriter())
{
using(XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xsl.Transform(xPathDoc, xmlWriter);
}
using(var reader = new StringReader(stringWriter.ToString()))
{
return new XPathDocument(reader);
}
}
}
Here's a quick question I've been banging my head against today.
I'm trying to convert a .Net dataset into an XML stream, transform it with an xsl file in memory, then output the result to a new XML file.
Here's the current solution:
string transformXML = #"pathToXslDocument";
XmlDocument originalXml = new XmlDocument();
XmlDocument transformedXml = new XmlDocument();
XslCompiledTransform transformer = new XslCompiledTransform();
DataSet ds = new DataSet();
string filepath;
originalXml.LoadXml(ds.GetXml()); //data loaded prior
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
transformer.Load(transformXML);
transformer.Transform(originalXml, writer); //no need to select the node
transformedXml.LoadXml(sb.ToString());
transformedXml.Save(filepath);
writer.Close();
Here's the original code:
BufferedStream stream = new BufferedStream(new MemoryStream());
DataSet ds = new DataSet();
da.Fill(ds);
ds.WriteXml(stream);
StreamReader sr = new StreamReader(stream, true);
stream.Position = 0; //I'm not certain if this is necessary, but for the StreamReader to read the text the position must be reset.
XmlReader reader = XmlReader.Create(sr, null); //Problem is created here, the XmlReader is created with none of the data from the StreamReader
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(#"<path to xsl file>");
transformer.Transform(reader, null, writer); //Exception is thrown here, though the problem originates from the XmlReader.Create(sr, null)
For some reason in the transformer.Transform method, the reader has no root node, in fact the reader isn't reading anything from the StreamReader.
My questions is what is wrong with this code? Secondarily, is there a better way to convert/transform/store a dataset into XML?
Edit: Both answers were helpful and technically aku's was closer. However I am leaning towards a solution that more closely resembles Longhorn's after trying both solutions.
I'm not sure but it seems that you didn't reset position in stream before passing it to XmlReader. Try to seek at the beginning of your stream before trying to read from it. Also it may be necessary to close\flush stream after you wrote some data to it.
EDIT:
Just tried following code and it worked perfectly:
BufferedStream stream = new BufferedStream(new MemoryStream());
stream.Write(Encoding.ASCII.GetBytes("<xml>foo</xml>"), 0, "<xml>foo</xml>".Length);
stream.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(stream);
XmlReader reader = XmlReader.Create(sr);
while (reader.Read())
{
Console.WriteLine(reader.Value);
}
stream.Close();
You must select the root node. This doesn't use Datasets, but I use this function everyday and it works great.
System.Xml.XmlDocument orgDoc = new System.Xml.XmlDocument();
orgDoc.LoadXml(orgXML);
// MUST SELECT THE ROOT NODE
XmlNode transNode = orgDoc.SelectSingleNode("/");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
System.IO.StringReader stream = new System.IO.StringReader(transformXML);
XmlReader reader = XmlReader.Create(stream);
System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform();
trans.Load(reader);
trans.Transform(transNode, writer);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
return doc;
please look it and use..
using (MemoryStream memStream = new MemoryStream())
{
memStream.Write(Encoding.UTF8.GetBytes(xmlBody), 0, xmlBody.Length);
memStream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(memStream))
{
// xml reader setting.
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings()
{
IgnoreComments = true,
IgnoreWhitespace = true,
};
// xml reader create.
using (XmlReader xmlReader = XmlReader.Create(reader, xmlReaderSettings))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoginInfo));
myObject = (LoginInfo)xmlSerializer.Deserialize(xmlReader);
}
}
}