Convert XmlText to HtmlText

I have been using Xml streams for quite some time. When I use them in an ASP.Net page, using SOAP or Ajax calls, I sometimes want to see what I send and what I recieve from the server. Showing them on the page was always a bit of tweak. This ample code demonstrates a very simple tweek. It provides a label that can show the xml Text (as its content or Text property) on the page, by rendering the simpel characters.
When the control is compiled I can use it as one of the two ways:

<panahyAjax:XmlLabel runat=”server” ID=”xmlLabel”>
<this>
 
<is just=”one”>sample</is>
</this>
</panahyAjax:XmlLabel>

Or

XmlLabel label = new XmlLabel();
var file = File.OpenText(“Sample.xml”);
label.Text = file.ReadToEnd();
And the code is as folllows:

public class XmlLabel : Label
{
 public static string ConvertXmlTextToHtmlText(string inputText)
 {
  // Replace all start and end tags.
  string startPattern = @”<([^>]+)>”;
  var regEx = new Regex(startPattern);
  string outputText = regEx.Replace(inputText, “&lt;<b>$1&gt;</b>”);
  outputText = outputText.Replace(
” “, “&nbsp;”);
  outputText = outputText.Replace(
“rn”, “<br />”);
  return outputText;
 }

 protected override void RenderContents(HtmlTextWriter output)
 {
  string xmlText = XmlLabel.ConvertXmlTextToHtmlText(Text);
  output.Write(xmlText);
 }
}

Get XML String from XElement

You can use the WriteTo method of the XElement object to an XmlWriter and this can be created in ten different ways like by giving a filename or passing a stream as output or simply giving the StringBuilder to write to:


/// <summary>
/// Generates XML string from an XElement
/// summary>
/// <param name="xml">XElement source</param>
public string GetXmlString(XElement xml)
{
// could also be any other stream
StringBuilder sb = new StringBuilder();

// Initialize a new writer settings
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration
= true;
xws.Indent
= true;

using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
// the actual writing takes place
xml.WriteTo(xw);
}

return sb.ToString();

}

It is important to know that the XElement already supports ToString which generates the same results as this code, but when you want to get something else, this could be a good start.


See further information in MSDN.

Another peace of code that I don’t want to rewrite it again (and again)

using System;
using System.Collections.Generic;

/// <summary>
/// This class uses XmlSerializer to do both sides of conversion to xml string
/// </summary>
/// <typeparam name="T">any Serializable class</typeparam>
public class Serializer<T>
{
/// <summary>
/// Convert an instance to XML string
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public string Serialize(T item)
{

MemoryStream ms
= new MemoryStream();
XmlTextWriter writer
= new XmlTextWriter(ms, Encoding.Unicode);

XmlSerializer serializer
= new XmlSerializer(typeof(T));
serializer.Serialize(writer, item);

writer.Flush();
ms.Position
= 0;

StreamReader reader
= new StreamReader(ms);
return reader.ReadToEnd();

}
/// <summary>
/// another version, could be better
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private string GetSerialize2(T item)
{
StringWriter wr
= new StringWriter(new StringBuilder());

XmlSerializer serializer
= new XmlSerializer(typeof(T));
serializer.Serialize(wr, item);

wr.Close();
return wr.GetStringBuilder().ToString();
}

/// <summary>
/// Convert the XML string to an instance of an object
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public T Deserialize(string xml)
{
XmlTextReader stream
= new XmlTextReader(new StringReader(xml));
XmlSerializer serializer
= new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(stream);
}
}

LINQ over XML

This example is also from th AppDev:


var doc = CreateDocument(); // returns an XDocument
var items = from item in doc.Descendants("Item")
where (string)item.Parent.Attribute("Name") == "Breads"
select item;

now transforming the xml to a new format:


XElement transformed = new XElement("items",
from item in items
select new XElement("item",
new XAttribute("ItemName", (string)item.Element("Name")),
new XAttribute("Price", (string)item.Element("Price))));

How to Transform A DataSet using Xml and Xslt in .Net 2.0

// Creating DOM Document
XmlDocument doc = new XmlDocument();

// Get hold of the XML string from the dataset
doc.LoadXml(ds.GetXml());

//Load XSL and Tranform
XslCompiledTransform myXslTrans = new XslCompiledTransform();
string path = Directory.GetCurrentDirectory();
myXslTrans.Load(path + @”PrintView.xslt“);

// Using a StringWriter for Streaming part
StringWriter stringWriter = new StringWriter();

// The actual transformation
myXslTrans.Transform(doc.DocumentElement, null, stringWriter);
stringWriter.Flush();

// Consuming the transformed results
webBrowser1.Document.Write(stringWriter.ToString());

//doc.Save(“Example.xml”);