Convert XML to JSON using C# and VB.Net in ASP.Net

satabeach
 
on Jul 24, 2022 10:58 PM
899 Views

How can I convert the below XML to JSON

<?xml version="1.0" encoding="UTF-8" ?>
<data>
    <attributes>
        <product_no>PR1154</product_no>
    </attributes>
    <relationships>
        <payables>
            <data>
                <attributes />
                <relationships>
                    <payable>
                        <data>
                            <type>product-type</type>
                            <id>1254</id>
                        </data>
                    </payable>
                </relationships>
            </data>
        </payables>
    </relationships>
</data>

expect output

{ "data": { "attributes": { "product_no": "PR1154" }, "relationships": { "payables": { "data": [ { "attributes": {}, "relationships": { "payable": { "data": { "type": "product-type", "id": "1254" } } } } ] } } } }

what I am getting

{ "data": { "attributes": { "product_no": "PR1154" "relationships": { "payables": { "data": { "attributes": null, "relationships": { "payable": { "data": { "type": "product-type", "id": "1254" } } } } } } } } }

 

        Dim postData As String = "<?xml version='1.0' encoding='UTF-8'?>" _
                                              & "<data>" _
                                              & "<attributes>" _
                                              & "<product_no>PR1154</product_no>" _
                                              & "<relationships>" _
                                              & "<payables>" _
                                              & "<data>" _
                                              & "<attributes />" _
                                              & "<relationships>" _
                                              & "<payable>" _
                                              & "<data>" _
                                              & "<type>product-type</type>" _
                                              & "<id>1254</id>" _
                                              & "</data>" _
                                              & "</payable>" _
                                              & "</relationships>" _
                                              & "</data>" _
                                              & "</payables>" _
                                              & "</relationships>" _
                                              & "</attributes>" _
                                              & "</data>"
        Dim doc As XmlDocument = New XmlDocument()
        doc.LoadXml(If(Me.TextBox1.Text = String.Empty, postData, Me.TextBox1.Text))

        Dim jsonTextd = JsonConvert.SerializeObject(doc.FirstChild.NextSibling, Newtonsoft.Json.Formatting.Indented)
        Me.TextBox1.Text = jsonTextd
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 26, 2022 12:31 PM

Hi satabeach,

Convert the XML to Class. Then use the class to Serialize to Json string.

Refer below example.

Namespaces

C#

using System.IO;
using System.Xml.Serialization;
using Newtonsoft.Json;

VB.Net

Imports System.IO
Imports System.Xml.Serialization
Imports Newtonsoft.Json

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string xml = "<?xml version='1.0' encoding='UTF-8' ?>              "
                    + "<data>                                               "
                    + "    <attributes>                                     "
                    + "        <product_no>PR1154</product_no>              "
                    + "    </attributes>                                    "
                    + "    <relationships>                                  "
                    + "        <payables>                                   "
                    + "            <data>                                   "
                    + "                <attributes />                       "
                    + "                <relationships>                      "
                    + "                    <payable>                        "
                    + "                        <data>                       "
                    + "                            <type>product-type</type>"
                    + "                            <id>1254</id>            "
                    + "                        </data>                      "
                    + "                    </payable>                       "
                    + "                </relationships>                     "
                    + "            </data>                                  "
                    + "        </payables>                                  "
                    + "    </relationships>                                 "
                    + "</data>";
        XmlSerializer serializer = new XmlSerializer(typeof(data));
        using (StringReader reader = new StringReader(xml))
        {
            string jsonTextd = JsonConvert.SerializeObject((data)serializer.Deserialize(reader), Formatting.Indented);                
        }
    }
}

public partial class data
{

    private dataAttributes attributesField;

    private dataRelationships relationshipsField;

    public dataAttributes attributes
    {
        get
        {
            return this.attributesField;
        }
        set
        {
            this.attributesField = value;
        }
    }

    public dataRelationships relationships
    {
        get
        {
            return this.relationshipsField;
        }
        set
        {
            this.relationshipsField = value;
        }
    }
}

public partial class dataAttributes
{

    private string product_noField;

    public string product_no
    {
        get
        {
            return this.product_noField;
        }
        set
        {
            this.product_noField = value;
        }
    }
}

public partial class dataRelationships
{

    private dataRelationshipsPayables payablesField;

    public dataRelationshipsPayables payables
    {
        get
        {
            return this.payablesField;
        }
        set
        {
            this.payablesField = value;
        }
    }
}

public partial class dataRelationshipsPayables
{

    private dataRelationshipsPayablesData dataField;

    public dataRelationshipsPayablesData data
    {
        get
        {
            return this.dataField;
        }
        set
        {
            this.dataField = value;
        }
    }
}

public partial class dataRelationshipsPayablesData
{

    private object attributesField;

    private dataRelationshipsPayablesDataRelationships relationshipsField;

    public object attributes
    {
        get
        {
            return this.attributesField;
        }
        set
        {
            this.attributesField = value;
        }
    }

    public dataRelationshipsPayablesDataRelationships relationships
    {
        get
        {
            return this.relationshipsField;
        }
        set
        {
            this.relationshipsField = value;
        }
    }
}

public partial class dataRelationshipsPayablesDataRelationships
{

    private dataRelationshipsPayablesDataRelationshipsPayable payableField;

    public dataRelationshipsPayablesDataRelationshipsPayable payable
    {
        get
        {
            return this.payableField;
        }
        set
        {
            this.payableField = value;
        }
    }
}

public partial class dataRelationshipsPayablesDataRelationshipsPayable
{

    private dataRelationshipsPayablesDataRelationshipsPayableData dataField;

    public dataRelationshipsPayablesDataRelationshipsPayableData data
    {
        get
        {
            return this.dataField;
        }
        set
        {
            this.dataField = value;
        }
    }
}

public partial class dataRelationshipsPayablesDataRelationshipsPayableData
{

    private string typeField;

    private ushort idField;

    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }

    public ushort id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim xml As String = "<?xml version='1.0' encoding='UTF-8' ?>" _
        & "<data>                                               " _
        & "    <attributes>                                     " _
        & "        <product_no>PR1154</product_no>              " _
        & "    </attributes>                                    " _
        & "    <relationships>                                  " _
        & "        <payables>                                   " _
        & "            <data>                                   " _
        & "                <attributes />                       " _
        & "                <relationships>                      " _
        & "                    <payable>                        " _
        & "                        <data>                       " _
        & "                            <type>product-type</type>" _
        & "                            <id>1254</id>            " _
        & "                        </data>                      " _
        & "                    </payable>                       " _
        & "                </relationships>                     " _
        & "            </data>                                  " _
        & "        </payables>                                  " _
        & "    </relationships>                                 " _
        & "</data>"
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(data))
        Using reader As StringReader = New StringReader(xml)
            Dim jsonTextd As String = JsonConvert.SerializeObject(CType(serializer.Deserialize(reader), data), Formatting.Indented)
        End Using
    End If
End Sub

Partial Public Class data
    Private attributesField As dataAttributes
    Private relationshipsField As dataRelationships

    Public Property attributes As dataAttributes
        Get
            Return Me.attributesField
        End Get
        Set(ByVal value As dataAttributes)
            Me.attributesField = value
        End Set
    End Property

    Public Property relationships As dataRelationships
        Get
            Return Me.relationshipsField
        End Get
        Set(ByVal value As dataRelationships)
            Me.relationshipsField = value
        End Set
    End Property
End Class

Partial Public Class dataAttributes
    Private product_noField As String

    Public Property product_no As String
        Get
            Return Me.product_noField
        End Get
        Set(ByVal value As String)
            Me.product_noField = value
        End Set
    End Property
End Class

Partial Public Class dataRelationships
    Private payablesField As dataRelationshipsPayables

    Public Property payables As dataRelationshipsPayables
        Get
            Return Me.payablesField
        End Get
        Set(ByVal value As dataRelationshipsPayables)
            Me.payablesField = value
        End Set
    End Property
End Class

Partial Public Class dataRelationshipsPayables
    Private dataField As dataRelationshipsPayablesData

    Public Property data As dataRelationshipsPayablesData
        Get
            Return Me.dataField
        End Get
        Set(ByVal value As dataRelationshipsPayablesData)
            Me.dataField = value
        End Set
    End Property
End Class

Partial Public Class dataRelationshipsPayablesData
    Private attributesField As Object
    Private relationshipsField As dataRelationshipsPayablesDataRelationships

    Public Property attributes As Object
        Get
            Return Me.attributesField
        End Get
        Set(ByVal value As Object)
            Me.attributesField = value
        End Set
    End Property

    Public Property relationships As dataRelationshipsPayablesDataRelationships
        Get
            Return Me.relationshipsField
        End Get
        Set(ByVal value As dataRelationshipsPayablesDataRelationships)
            Me.relationshipsField = value
        End Set
    End Property
End Class

Partial Public Class dataRelationshipsPayablesDataRelationships
    Private payableField As dataRelationshipsPayablesDataRelationshipsPayable

    Public Property payable As dataRelationshipsPayablesDataRelationshipsPayable
        Get
            Return Me.payableField
        End Get
        Set(ByVal value As dataRelationshipsPayablesDataRelationshipsPayable)
            Me.payableField = value
        End Set
    End Property
End Class

Partial Public Class dataRelationshipsPayablesDataRelationshipsPayable
    Private dataField As dataRelationshipsPayablesDataRelationshipsPayableData

    Public Property data As dataRelationshipsPayablesDataRelationshipsPayableData
        Get
            Return Me.dataField
        End Get
        Set(ByVal value As dataRelationshipsPayablesDataRelationshipsPayableData)
            Me.dataField = value
        End Set
    End Property
End Class

Partial Public Class dataRelationshipsPayablesDataRelationshipsPayableData
    Private typeField As String
    Private idField As UShort

    Public Property type As String
        Get
            Return Me.typeField
        End Get
        Set(ByVal value As String)
            Me.typeField = value
        End Set
    End Property

    Public Property id As UShort
        Get
            Return Me.idField
        End Get
        Set(ByVal value As UShort)
            Me.idField = value
        End Set
    End Property
End Class

Screenshot

Generated Json