0

I have a XML like this:

<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
    <SINGLE>
        <KEY name="grades">
            <MULTIPLE>
                <SINGLE>
                    <KEY name="courseid">
                        <VALUE>3</VALUE>
                    </KEY>
                    <KEY name="grade">
                        <VALUE>40.00</VALUE>
                    </KEY>
                    <KEY name="rawgrade">
                        <VALUE>40.00000</VALUE>
                    </KEY>
                    <KEY name="rank">
                        <VALUE null="null"/>
                    </KEY>
                </SINGLE>
            </MULTIPLE>
        </KEY>
        <KEY name="warnings">
            <MULTIPLE></MULTIPLE>
        </KEY>
    </SINGLE>
</RESPONSE>

I have converted it to JSON string like with below

string json = JsonConvert.SerializeXmlNode(doc);

But I need to assign my JSON into object of the below class type

public class Grades
{
    public string Courseid { get; set; }
    public string Grade{ get; set; }
    public string Rawgrade{ get; set; }
    public string Rank{ get; set; }
}

So that, I can access or assign my values into

Grade mygrade;

or later mygrade.Courseid

and now it looks like this:

{
  "?xml": {
    "@version": "1.0",
    "@encoding": "UTF-8"
  },
  "RESPONSE": {
    "SINGLE": {
      "KEY": [
        {
          "@name": "grades",
          "MULTIPLE": {
            "SINGLE": {
              "KEY": [
                {
                  "@name": "courseid",
                  "VALUE": "3"
                },
                {
                  "@name": "grade",
                  "VALUE": "40.00"
                },
                {
                  "@name": "rawgrade",
                  "VALUE": "40.00000"
                },
                {
                  "@name": "rank",
                  "VALUE": {
                    "@null": "null"
                  }
                }
              ]
            }
          }
        },
        {
          "@name": "warnings",
          "MULTIPLE": ""
        }
      ]
    }
  }
}

But I need the JSON like the following

{
  "courseid" : "3",
  "grade": "40.00",
  "rawgrade": "40.00000",
  "rank": "null"
}

How can I assign properly my json to the above class format Grades? Can anyone please help me? I am new to ASP.NET Core.

9
  • use dyanmic object Commented Oct 11, 2021 at 7:31
  • 1
    Can you detail what make this question unique to ASP.NET Core? Are you looking to do this as a request is received from the client, so that the controller method receives the final object? Commented Oct 11, 2021 at 7:33
  • You cannot convert XML to JSON - at least not the way you want it to. You have to de-serialize the xml to an object and then serialize that to JSON. Apart from this why should your Repsonse-object be some anonymous class? Commented Oct 11, 2021 at 7:44
  • @Llama yes added to question, please have a look Commented Oct 11, 2021 at 9:01
  • 1
    I would use LinqToXml and load the data into the object directly from XML. Without intermediate Json. Commented Oct 11, 2021 at 10:51

1 Answer 1

2

Based on your input xml and output json, you cannot directly convert to JSON. Here is one approach using LINQ to get desired object and Serialize using JsonConvert

var strFile = File.ReadAllText("XMLFile1.xml");

var grades = xdoc.Descendants("KEY")
                 .Where(x => x.Attribute("name").Value == "grades")
                 .Descendants("SINGLE")
                 .Select(y => new Grades
                  {
                        Courseid = y.Descendants("KEY").Where(z => z.Attribute("name").Value == "courseid").Select(a => a.Element("VALUE").Value).FirstOrDefault(),
                        Grade = y.Descendants("KEY").Where(z => z.Attribute("name").Value == "grade").Select(a => a.Element("VALUE").Value).FirstOrDefault(),
                        Rawgrade = y.Descendants("KEY").Where(z => z.Attribute("name").Value == "rawgrade").Select(a => a.Element("VALUE").Value).FirstOrDefault(),
                        Rank = y.Descendants("KEY").Where(z => z.Attribute("name").Value == "rank").Select(a => a.Element("VALUE").Value).FirstOrDefault(),
                  });
                    
Console.WriteLine(JsonConvert.SerializeObject(grades));
Sign up to request clarification or add additional context in comments.

2 Comments

please have a look to my updated question
@ShuvoBarua I have updated the post. Hope this helps

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.