Feed on
Posts
Comments

Archive for the ‘XML’ Category

Flatten A Node In XSLT

<xsl:for-each select=“MyNode“>
  <MyNode>
    <xsl:for-each select=“@*|*[not(* or @*)]“>
      <xsl:attribute name=“{name(.)}“>
        <xsl:value-of select=“.“/>
      </xsl:attribute>
    </xsl:for-each>
  </MyNode>
</xsl:for-each>
Where MyNode:
<MyNode>value1</MyNode>
<MyNode>value2</MyNode>
<MyNode>value3</MyNode>

Read Full Post »

Writing C# Code In Xslt

Many things cannot be done in xslt directly due to a lack of manipulation functions. In order to cure that, Microsoft has provided a way to include c# code directly in xslt. Here’s an example:
<?xml version=“1.0“ encoding=“utf-8“ ?>
  <xsl:stylesheet version=“1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform“ xmlns:msxsl=“urn:schemas-microsoft-com:xslt“ xmlns:ClassNameHere=“dummy namespace here“>
    <msxsl:script language=“C#“ implements-prefix=“ClassNameHere“>
      <![CDATA[
        public string MethodName(/* some parameters here*/)
        [...]

Read Full Post »

Copy XML As Is Upon Transformation

<?xml version=“1.0“ encoding=“UTF-8“ ?>
<xsl:stylesheet version=“1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform“>
  <xsl:output method=“xml“/>
  <xsl:template match=“/“ >
    <xsl:copy-of select=“.“/>
  </xsl:template>
</xsl:stylesheet>

Read Full Post »

Deserialize XML string

Use this following static method to convert any xml to its object representation 
public static object ToObject(Type objectType, string xml)
{
  object obj = null;
  try
  {
    XmlSerializer xs = new XmlSerializer(objectType);
    obj = xs.Deserialize(new StringReader(xml));
  }
  catch (Exception ex)
  {
  }
  return obj;
}

Read Full Post »

Serialize XML objects

 Use this following static method to convert any serilizable object into its string representation 
public static string ToString(object xmlObject)
{
  XmlSerializer xs = new XmlSerializer(xmlObject.GetType());
  StringBuilder sb = new StringBuilder();
  StringWriter sw = new StringWriter(sb);
  string str = string.Empty;
  try
  {
    xs.Serialize(sw, xmlObject);
    str = sw.ToString();
  }
  catch (Exception ex)
  {
  }
  return str;
}

Read Full Post »