Link to home
Start Free TrialLog in
Avatar of Tom Sage
Tom SageFlag for United States of America

asked on

Creating and using List of Expando objects

I am trying to create a Json object in C# that I will convert to XML.   I have a repeating set of Fees that I need to add to the json object.

Here is my code so far:

root.Deal.Fees = new ExpandoObject();
List<ExpandoObject> feeArray = new List<ExpandoObject>();

foreach (DataRow ro in ds.Tables["fees"].Rows)
{
    root.Deal.Fees.DealFees.Fee.Description = ro["fee_type"].ToString();;
    root.Deal.Fees.DealFees.Fee.Amount = ro["amount"].ToString();

feeArray.Add(root.Deal.Fees);
}

Open in new window


No sure what else is needed.   Supply a different example if that is easier for you.
Thank you
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

I have been using Newtonsoft.Json for the past couple of months with great success. You can add it via NuGet. Just search for Newtonsoft.Json and add it to your solution. You will then need to add the Newtonsoft namespace to your code.

You can then easily serialize and deserialize your feeArray object. Have a look at this link: http://www.newtonsoft.com/json/help/html/SerializingJSON.htm

But if it is XML that you are after, have a look at doing it directly: https://support.microsoft.com/en-ph/help/815813/how-to-serialize-an-object-to-xml-by-using-visual-c

So I'm not sure if you want it serialized to JSON or XML. Json is smaller in size than XML. Many other languages can consume Json out of the box, so you need to make a decision based on your unique situation.

Lastly, if you use Newtonsoft.Json, you can perform LINQ queries against your JSON object, which is quite cool.
Avatar of Tom Sage

ASKER

Hi Dirk - I will investigate those links.

My input is json and output is XML.  

I will update this topic by tomorrow.

Thanks
If your input is Json, use Newtonsoft.Json to deserialize the Json to an object and then serialize that object to XML. I'm not sure if there is a direct way to go from Json to XML.
ASKER CERTIFIED SOLUTION
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great I will check it out.   Thanks
Thanks for your help Dirk !
Only solution