0

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?

1
  • Answered, edited anmy answer recently, added dynamic json generation code sample and dynamic class Commented Feb 25, 2019 at 0:48

1 Answer 1

1
  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 } 
    }
  },
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.