AlphaWolf
AlphaWolf

Reputation: 181

C# Parse Dynamic xML into objects and return as JSON

I have XML payloads in blob storage and am looking for the best way to parse this into classes in C#. I want to be able to parse the XML in an API and return json objects based on the XML payload however, I am unsure the best approach to start this.

How would I go about doing this?

Upvotes: 0

Views: 303

Answers (1)

Default Writer
Default Writer

Reputation: 2566

  1. Read the documentation,
  2. You can use XmlDocument and use ValidationType.DTD in DTD processing with XmlReaderSettings to parse and validate XML document agains DTDs
  3. Look into simiar question here
  4. You can use NewtonSoft JSON serialization library to serialize objects into JSON format
  5. Additionally, you can use dynamically generated JSON,
class cXMLJsonNode : Dictionary<string,object> 
{
}

to create custom built JSON object:

JsonConvert.SerializeObject(new cXMLJsonNode {
  { key1, value1 },
  { key2, value2 },
  { property1, new cXMLJsonNode {
    { key1, oldValue1 } 
    { key2, oldValue1 } 
  },
  { property2, new cXMLJsonNode {
    { key1, newValue1 } 
    { key2, new cXMLJsonNode {
      { key1, newValue1 } 
      { key2, newValue2 } 
    }
  },
})

Upvotes: 1

Related Questions