Wednesday, December 12, 2012

Convert JSON to XML

You may come across situation when you receive data in JSON and would like to convert into XML. JSON (JavaScript Object Notation) is a text-based open standard designed for human-readable data interchange.

 

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML.

 

Following example is a function which converts a JSON data string into XML Document.

 

Private Shared Function JsonToXml(ByVal json As String) As XmlDocument
        Dim newNode As XmlNode = Nothing
        Dim appendToNode As XmlNode = Nothing
        Dim returnXmlDoc As XmlDocument = New XmlDocument
        returnXmlDoc.LoadXml("<Document />")
        Dim rootNode As XmlNode = returnXmlDoc.SelectSingleNode("Document")
        appendToNode = rootNode
 
        json = json.Replace(",", "," & vbLf)
        json = json.Replace("{", "{" & vbLf)
        json = json.Replace("}", vbLf & "}" & vbLf)
        json = json.Replace("[", "[" & vbLf)
        json = json.Replace("]", vbLf & "]" & vbLf)
 
        Dim arrElements() As String = json.Split(CChar(vbLf))
        For Each element As String In arrElements
            Dim processElement As String = element.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "").Trim()
            If processElement.Trim <> "" Then
                If (processElement.IndexOf("}") > -1 Or processElement.IndexOf("]") > -1) And Not (appendToNode Is rootNode) Then
                    appendToNode = appendToNode.ParentNode
                ElseIf (processElement.IndexOf("[") > -1) Then
                    processElement = processElement.Replace(":", "").Replace("[", "").Replace("""", "").Trim()
                    If processElement.Trim = "" Then
                        processElement = "Square"
                    End If
                    newNode = returnXmlDoc.CreateElement(processElement)
                    appendToNode.AppendChild(newNode)
                    appendToNode = newNode
                ElseIf (processElement.IndexOf("{") > -1) Then
                    processElement = processElement.Replace(":", "").Replace("{", "").Replace("""", "").Trim()
                    If processElement.Trim = "" Then
                        processElement = "Element"
                    End If
                    newNode = returnXmlDoc.CreateElement(processElement)
                    appendToNode.AppendChild(newNode)
                    appendToNode = newNode
                Else
                    Dim FoundAt As Integer = processElement.IndexOf(":")
                    If (FoundAt > -1) Then
                        Dim NodeName As String = processElement.Substring(0, FoundAt - 1).Replace("""", "")
                        Dim NodeValue As String = processElement.Substring(FoundAt + 1, processElement.Length - FoundAt - 1).Trim
                        If NodeValue.StartsWith("""") Then
                            NodeValue = NodeValue.Substring(1, NodeValue.Length - 1).Trim
                        End If
                        If NodeValue.EndsWith(",") Then
                            NodeValue = NodeValue.Substring(0, NodeValue.Length - 1).Trim
                        End If
                        If NodeValue.EndsWith("""") Then
                            NodeValue = NodeValue.Substring(0, NodeValue.Length - 1).Trim
                        End If
                        newNode = returnXmlDoc.CreateElement(NodeName)
                        newNode.InnerText = NodeValue
                        appendToNode.AppendChild(newNode)
                    End If
                End If
            End If
        Next
        Return returnXmlDoc
    End Function

Monday, November 26, 2012

Reading XML with Namespace

If your XML contains Namespace information, then following example illustrates as how to read the data

Let’s consider following XML saved as TestResp.xml

This example contains elements and attribute.

<ResponseData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Result xmlns="http://TestInfo.com/GetData">
<Data>
<Id>3260</Id>
<StatusCode>0</StatusCode>
<StatusValue>NA</StatusValue>
<Values>
    <Value Type="A">1</Value>
    <Value Type="B">1</Value>
</Values>
</Data>
</Result>
</ResponseData>

Now, let’s read the info

Dim xmlString As String = ""
If File.Exists("C:\Test\TestResp.xml") Then xmlString = File.ReadAllText("C:\Test\TestResp.xml")
 
If String.IsNullOrEmpty(xmlString) = False Then
    xmlDoc = New Xml.XmlDocument
    xmlDoc.LoadXml(xmlString)
    Dim ns1 As New Xml.XmlNamespaceManager(xmlDoc.NameTable)
    ns1.AddNamespace("t", "http://TestInfo.com/GetData")
 
    If xmlDoc.SelectSingleNode("//t:Result/t:Data/t:Id", ns1) IsNot Nothing AndAlso _
                   IsNumeric(xmlDoc.SelectSingleNode("//t:Result/t:Data/t:Id", ns1).InnerText) Then
        Dim id As String = xmlDoc.SelectSingleNode("//t:Result/t:Data/t:Id", ns1).InnerText
    End If
 
    If xmlDoc.SelectSingleNode("//t:Result/t:Data/t:Values/t:Value", ns1) IsNot Nothing Then
        Dim xmlnodes As Xml.XmlNodeList = xmlDoc.SelectNodes("//t:Result/t:Data/t:Values/t:Value", ns1)
        For Each nod As Xml.XmlNode In xmlnodes
            If nod.Attributes("Type").Value = "A" Then
                Dim valueTypeA As String = nod.InnerText
            End If
        Next
    End If
End If

Saturday, October 06, 2012

Error: The underlying connection was closed

This is one of the most interesting error you may come across specially when using WCF Services. It will give you no detail, just go figure out - The underlying connection was closed: The connection was closed unexpectedly. So what to do? Most likely you have dataset size issue which is going back and forth between different tiers. For the security and performance reasons you should always limit the size. But if you are required to pass big chunk of data, you have to change the size to it’s maximum. So how do we do it?

 

You have to change the configuration on both side (client and service).

 

On the WCF Service side: ensure that you have

 

<dataContractSerializer maxItemsInObjectGraph="2147483647" />

 

2GB is the maximum size you can send data at once using WCF Service, so please be careful, you can adjust to a lower value as needed

 

<behaviors>
       <serviceBehaviors>
          <behavior name="serviceBehavior_Name">             
            <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          </behavior>
       </serviceBehaviors>
</behaviors>

On the Client Side: Make sure that Binding and behavior info are set correctly

e.g. Binding

<binding name="wsHttpEndpoint_ITestService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
         <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
         <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
         <security mode="Message">
           <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
           <message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="true"/>
         </security>
</binding>

Client Behavior

<behaviors>
     <endpointBehaviors>
       <behavior name="endpointBehavior_Common">
         <dataContractSerializer maxItemsInObjectGraph="2147483647" />
       </behavior>
     </endpointBehaviors>
   </behaviors>

Finally, Client where you refer the actual service

<client>
    <endpoint address="http://www.test.com/TestService.svc" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpoint_ITestService" contract="TestService.ITestService" name="wsHttpEndpoint_ITestService"  behaviorConfiguration="endpointBehavior_Common" />            
</client>

This should fix the above problem.

Thursday, September 06, 2012

WCF Diagnostics

Have you seen weird error messages while using WCF Services and unable to figure out easily as what’s going on? Add the following diagnostics configuration in you config file under configuration section, may be at the bottom. It will generate a file named “App_tracelog.svclog”, you can open as XML file and read the info and may be able to trace the problem easily.

<system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
        <listeners>
          <add name="ServiceModelTraceListener"/>
        </listeners>
      </source>
 
      <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
        <listeners>
          <add name="ServiceModelTraceListener"/>
        </listeners>
      </source>
      <source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing">
        <listeners>
          <add name="ServiceModelTraceListener"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="App_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp"/>
    </sharedListeners>
  </system.diagnostics>

The file will be generated on the same level where config file is available. Make sure to either comment or remove this configuration once you resolved the issue else it may impact the performance and use unnecessary disk space.

Monday, April 09, 2012

Abstract Factory Design Pattern

It is one of the highly used Creational Pattern from Gang of Four patterns. It provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the concrete objects that are part of the theme.

 

The client does not know (or care) which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage.

 

When and where to use it

You may wonder why you want to create objects using another class (called Abstract Factory) rather than calling constructors directly. Here are some reasons:

  1. Constructors are limited in their control over the overall creation process. If an application needs more control, using Factory is best option. E.g. sharing and re-using objects like object caching, or applications that maintain object and type counts.
  2. There are times when the client does not know exactly what type to construct to use. It is easier to code against a base type or an interface and then let a factory make the decision. ADO.NET objects such as DBConnection, DBCommand etc. are the examples.
  3. Constructors don’t communicate their intention very well because they must be named after their class. Having numerous overloaded constructors may make it hard for the developer to decide which constructor to use. Replacing constructors with intention-revealing creation methods are frequently preferred. 

 

Steps to Create Abstract Factory

  1. First Create Abstract Objects Interfaces (e.g. IAbstractProduct1, IAbstractProduct2) – It is like making of group of objects which will implement the set of same interfaces
  2. Create an Abstract Factory Interface (e.g. IAbstractFactory) which defines the Abstract Object Interfaces created in step 1
  3. Ensure actual concrete object (e.g. Product11 , Product12, Product21 and Product22) implements one of the Abstract Object Interface as defined in step1
  4. Finally Create Concrete Factories(e.g. ConcreteFactory1, ConcreteFactory2) which implements Abstract Factory Interface defined in step 2

 

Sample Code

Step 1.

''' <summary>
''' Abstract Product 1 (Group)
''' </summary>
''' <remarks></remarks>
Public Interface IAbstractProduct1
 
End Interface
 
''' <summary>
''' Abstract Product 2 (Group)
''' For example Here Abstract Product 2 Group is using AbtractProduct1 
''' </summary>
Public Interface IAbstractProduct2
    Function Status(ByVal ab1 As IAbstractProduct1) As String
End Interface

 

Step 2.

''' <summary>
''' Highest Level of Abstraction
''' The Abstract Factory
''' It defines two Abstract Product Groups (1 and 2)
''' </summary>
''' <remarks></remarks>
Interface IAbstractFactory
    Function CreateABF1() As IAbstractProduct1
    Function CreateABF2() As IAbstractProduct2
End Interface

 

Step 3.

''' <summary>
''' Product11 is part of AbstractProduct1 Group
''' </summary>
''' <remarks></remarks>
 
Public Class Product11
    Implements IAbstractProduct1
 
End Class
 
Public Class Product12
    Implements IAbstractProduct1
 
End Class
 
Public Class Product21
    Implements IAbstractProduct2
 
    Public Function SendStatus(ab1 As IAbstractProduct1) As String Implements IAbstractProduct2.Status
        Dim msgString As String = Me.GetType().Name & " is using " & CType(ab1, Object).GetType().Name
        Return msgString
    End Function
End Class
 
Public Class Product22
    Implements IAbstractProduct2
 
    Public Function SendStatus(ab1 As IAbstractProduct1) As String Implements IAbstractProduct2.Status
        Dim msgString As String = Me.GetType().Name & " is using " & CType(ab1, Object).GetType().Name
        Return msgString
    End Function
End Class

 

Step 4

''' <summary>
''' Abstract Factory gets implemented using the Concrete Factory
''' These methods finally gets implemented to return the respective product or object
''' </summary>
''' <remarks></remarks>
Public Class ConcreteFactory1
    Implements IAbstractFactory
 
    Public Function CreateABF1() As IAbstractProduct1 Implements IAbstractFactory.CreateABF1
        Return New Product11()
    End Function
 
    Public Function CreateABF2() As IAbstractProduct2 Implements IAbstractFactory.CreateABF2
        Return New Product21()
    End Function
End Class
 
''' <summary>
''' Abstract Factory gets implemented using the Concrete Factory
''' These methods finally gets implemented to return the respective product or object
''' </summary>
''' <remarks></remarks>
 
Public Class ConcreteFactory2
    Implements IAbstractFactory
 
    Public Function CreateABF1() As IAbstractProduct1 Implements IAbstractFactory.CreateABF1
        Return New Product12
    End Function
 
    Public Function CreateABF2() As IAbstractProduct2 Implements IAbstractFactory.CreateABF2
        Return New Product22
    End Function
End Class

 

Client to Test this Design Pattern

'You can use either ConcreateFactory for this test - Using a Windows app client
        'Dim factory As New ConcreteFactory1
        Dim factory As New ConcreteFactory2
        Dim prod1 = factory.CreateABF1
        Dim prod2 = factory.CreateABF2
        MsgBox(prod2.Status(prod1))