0

Please help me parse this file and get the output as JSON. So far I am able to get the key (name) and Value (value) pair. But I don't understand how to get the array inside this key-value pair. New to XML and JSON and C#. Guru's please help..

<?xml version="1.0" encoding="US-ASCII"?>
<Information>
  <first_name>Frank</first_name>
  <last_name>Murphy</last_name>
  <customer_support_url>google.com</customer_support_url>
  <received_date>12/30/2013</received_date>
  <customer_support_phone_number>(888)111-2222</customer_support_phone_number>
  <employer_name>Google Inc</employer_name>
  <label>Dependent Care</label>
  <set_flag>0</set_flag>
  <employee_id>11111</employee_id>
  <employer_id>111</employer_id>
  <claim_form_id>1234</claim_form_id>
  <employer_url>For more information please go to: &lt;a href=http://google.com&gt;google.com&lt;/a&gt;</employer_url>
  <tax>$1,500.00</tax>
  <claim_form_id>1234</claim_form_id>
  <claim_details>
      <claim_form_id>1234</claim_form_id>
      <amount_claimed>100</amount_claimed>
      <expense_name>ChildCare</expense_name>
      <service_date>2013-06-17</service_date> 
      <claim_status>Approved</claim_status>
      <claim_status_reason/>
  </claim_details>
  <Location>Dublin</Location>

So far this is what I have done for testing purpose in a console application:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.IO;
 using System.Xml;
 using System.Xml.Linq;

 namespace ConsoleApplication1
 {
    class Program
    {
        static void Main(string[] args)
        {
           XDocument doc = XDocument.Load("input.xml");
           // Declare your token Colelction Class 

           foreach (var keyvalue in doc.Root.DescendantNodes().OfType<XElement>().Select(x => new XElement("token", new XElement("name", x.Name), new XElement("value", x.Value))))
           {
               Console.WriteLine(keyvalue);

               //Add name,value to token collection
           }
           // Convert it to json
           //return

           Console.ReadKey();

         }
    }
  }
Ditty
  • 521
  • 7
  • 24
  • The class object has to be in such a way that we can add a key-value element in my XML without having to change the class object. – Ditty Jul 19 '14 at 02:25

2 Answers2

3

You can use Json.NET's JsonConvert.SerializeXmlNode() to convert XML to JSON easily :

XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
string jsonText = JsonConvert.SerializeXmlNode(doc);

Result :

{
   "Information": {
      "first_name": "Frank",
      "last_name": "Murphy",
      "customer_support_url": "google.com",
      "received_date": "12/30/2013",
      "customer_support_phone_number": "(888)111-2222",
      "employer_name": "Google Inc",
      "label": "Dependent Care",
      "set_flag": "0",
      "employee_id": "11111",
      "employer_id": "111",
      "claim_form_id": [
         "1234",
         "1234"
      ],
      "employer_url": "For more information please go to: <a href=http://google.com>google.com<\/a>",
      "tax": "$1,500.00",
      "claim_details": {
         "claim_form_id": "1234",
         "amount_claimed": "100",
         "expense_name": "ChildCare",
         "service_date": "2013-06-17",
         "claim_status": "Approved",
         "claim_status_reason": null
      },
      "Location": "Dublin"
   }
}
har07
  • 88,338
  • 12
  • 84
  • 137
  • How do I install the Json.Net? – Ditty Jul 19 '14 at 02:34
  • I used Nuget Visual Studio package manager, you can also download it manually from http://james.newtonking.com/json – har07 Jul 19 '14 at 02:37
  • Ok. Thank you. Let me try this – Ditty Jul 19 '14 at 02:38
  • I should be able to send it via JSON or XML depending on the content header that the client is asking. So is there anyway to tweak this to return based on JSON or XML? – Ditty Jul 19 '14 at 02:39
  • I tried and it does seem to work. But I am getting data very similar to the post as mentioned here: http://stackoverflow.com/questions/6861708/how-to-serialize-xml-to-a-json-object-with-json-net – Ditty Jul 19 '14 at 04:44
1

First convert your XML to classes, in vs edit pasted special paste XML as classes gives this (ps you've missed the closing line in your xml )

 /// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
 [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Information
{

private object[] itemsField;

private ItemsChoiceType[] itemsElementNameField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Location", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("claim_details", typeof(InformationClaim_details))]
[System.Xml.Serialization.XmlElementAttribute("claim_form_id", typeof(ushort))]
[System.Xml.Serialization.XmlElementAttribute("customer_support_phone_number", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("customer_support_url", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("employee_id", typeof(ushort))]
[System.Xml.Serialization.XmlElementAttribute("employer_id", typeof(byte))]
[System.Xml.Serialization.XmlElementAttribute("employer_name", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("employer_url", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("first_name", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("label", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("last_name", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("received_date", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("set_flag", typeof(byte))]
[System.Xml.Serialization.XmlElementAttribute("tax", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items
{
    get
    {
        return this.itemsField;
    }
    set
    {
        this.itemsField = value;
    }
  }

  /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
  [System.Xml.Serialization.XmlIgnoreAttribute()]
  public ItemsChoiceType[] ItemsElementName
  {
    get
    {
        return this.itemsElementNameField;
    }
    set
    {
        this.itemsElementNameField = value;
    }
   }
  }

 /// <remarks/>
 [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class InformationClaim_details
{

private ushort claim_form_idField;

private byte amount_claimedField;

private string expense_nameField;

private System.DateTime service_dateField;

private string claim_statusField;

private object claim_status_reasonField;

/// <remarks/>
public ushort claim_form_id
{
    get
    {
        return this.claim_form_idField;
    }
    set
    {
        this.claim_form_idField = value;
    }
}

/// <remarks/>
public byte amount_claimed
{
    get
    {
        return this.amount_claimedField;
    }
    set
    {
        this.amount_claimedField = value;
    }
}

/// <remarks/>
public string expense_name
{
    get
    {
        return this.expense_nameField;
    }
    set
    {
        this.expense_nameField = value;
    }
}

 /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
 public System.DateTime service_date
  {
    get
    {
        return this.service_dateField;
    }
    set
    {
        this.service_dateField = value;
    }
}

/// <remarks/>
public string claim_status
{
    get
    {
        return this.claim_statusField;
    }
    set
    {
        this.claim_statusField = value;
    }
  }

  /// <remarks/>
   public object claim_status_reason
  {
    get
    {
        return this.claim_status_reasonField;
    }
    set
    {
        this.claim_status_reasonField = value;
       }
    }
}

Then de-serialise the XML to c# objects

XmlSerializer serializer = new XmlSerializer(typeof(Information));

convert to JSon string Json= JsonConvert.SerializeObject(ObjectInstance);

fuzzybear
  • 2,325
  • 3
  • 23
  • 45
  • The issue is the elements can change. So I cannot make my class object dependent on the elements like you have done above. – Ditty Jul 19 '14 at 02:23
  • The element attributes can change. So cannot hard code those values – Ditty Jul 19 '14 at 02:26