2

I have an XML like this:

       <job>
           <properties>
              <name>jobid</name>
              <value>81963</value>
           </properties>
           <properties>
              <name>status</name>
              <value>complete</value>
           </properties>
           <properties>
              <name>date</name>
              <value>2018-07-30</value>
           </properties>
        </job>
        <job>
           <properties>
              <name>jobid</name>
              <value>81194</value>
           </properties>
           <properties>
              <name>status</name>
              <value>complete</value>
           </properties>
           <properties>
              <name>date</name>
              <value>2018-07-30</value>
           </properties>
        </job>

And what I need to happen is to get all the properties of each job. I have had a hard time looping through its nodes and childnodes but couldn't get the exact logic of this. What I really need to happen is to convert this data to something like:

[{
  "jobid": "81963",
  "status": "complete",
  "date": "2018-07-30"
}, 
{
  "jobid": "81194",
  "status": "complete",
  "date": "2018-07-30"
}]

I have already tried this:

foreach (XmlNode child in xn.SelectNodes("properties"))
            {
                arrd.Add(checkNullValue(child["value"]));
            }
            arrd2.AddRange(arrd);

//For Converting to JSON
 try
            {
                var jobVals = getXmlData("test", "testuser2", "654321", "Sources/soapRequest.xml");
            Response.Write(jobVals.Count);

            //JSONIZE list(the XML)
            string json = JsonConvert.SerializeObject(jobVals);
            Response.Write(json);
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }

Please Help.

OneLazy
  • 499
  • 4
  • 16

3 Answers3

4

Using Linq to XML

XDocument xml = //...

var result = xml.Elements("job")
    .Select(job => job
        .Elements("properties")
        .ToDictionary(p => p.Element("name").Value, p => p.Element("value").Value)
    );

result will contain a collection of dictionaries, that when serialized,

string json = JsonConvert.SerializeObject(result);

using something like Json.Net, will produce the desired result.

The above example does not include any validation checks, but that can be easily added. The example was just to show how the data can be transformed into the desired model.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

The JSON you have provided is not a valid JSON. Where as you can do this using newtonsoft

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • My bad for the JSON. I have updated the json string and is now valid. I actually have no problem in converting the data to JSON. The thing is I have a hard time have somthing to convert I mean building the list I need to pass to newtonsoft. – OneLazy Aug 06 '18 at 03:19
  • Does this method produce JSON with desired format? I think this will be `[{"properties":[{"name":"jobid","value":"81963"}]},..]` – vasily.sib Aug 06 '18 at 03:20
  • @vasily.sib: You are absolutely correct. It wont produce the JSON with desired format. – Mohit S Aug 06 '18 at 03:22
  • @OneLazy: Can you show what is your result and what is expected. I hope you are talking about `jobs.Add` – Mohit S Aug 06 '18 at 03:25
  • @MohitShrivastava I updated my code from above again please take a look. Witrh that im getting an output of : ["81963","complete","2018-07-30","81194","complete","2018-07-30"] but still my desired output is the JSON I put from above – OneLazy Aug 06 '18 at 03:36
0

Very easy in XSLT:

<xsl:template match="/">[<xsl:apply-templates/>]</xsl:template>
<xsl:template match="job">{<xsl:apply-templates/>}</xsl:template>
<xsl:template match="properties">"<xsl:value-of select="name"/>":"<xsl:value-of select="value"/>"</xsl:template>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164