0

I have following xml;

<?xml version="1.0" encoding="utf-8" ?>
<XslMapper>
  <type name="article" xsl="http://localhost:8080/Xsl-a.xslt">
    <category name="1234" xsl="http://localhost:8080/Xsl-b.xslt"></category>
    <category name="1234" xsl="http://localhost:8080/Xsl-b.xslt"></category>
  </type>
  <type name="slideshow" xsl="http://localhost:8080/Xsl-c.xslt" >
    <category name="1234" xsl="http://localhost:8080/Xsl-b.xslt"></category>
  </type>
</XslMapper>

C# code for parsing;

WebClient client = new WebClient();
            StringBuilder builder = new StringBuilder();
            string downloadString = client.DownloadString(XslMapperFileAddress);
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(downloadString);
            XmlWriter writer = XmlWriter.Create(builder, new XmlWriterSettings() { OmitXmlDeclaration = true });
            xml.Save(writer);
            string xmlString = builder.ToString();
            xml.LoadXml(xmlString);
            string jsonText = JsonConvert.SerializeXmlNode(xml, Formatting.Indented, true);
            jsonText = Regex.Replace(jsonText, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase);
            XslMapper xslMapper = JsonConvert.DeserializeObject<XslMapper>(jsonText);
            return xslMapper.XmlMapperTypes;

When I serialize this xml into json with json.net I am getting following result;

{
  "type": [
    {
      "name": "article",
      "xsl": "http://localhost:8080/Services/Xsl-a.xslt",
      "category": [
        {
          "name": "1234",
          "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
        },
        {
          "name": "1234",
          "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
        }
      ]
    },
    {
      "name": "slideshow",
      "xsl": "http://localhost:8080/Services/Xsl-c.xslt",
      "category": {
        "name": "1234",
        "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
      }
    }
  ]
}

as you can see first category section is parsed as an array (which I am intended to do) and second part converted as object. That's why I am getting error from JSON.NET

How can I parse second part as array like;

"category": [
        {
          "name": "1234",
          "xsl": "http://localhost:8080/Services/Xsl-b.xslt"
        }        
      ]
    },
1
  • What is the XslMapper class? Is it one you created or is it part of a 3rd party library? Commented Apr 1, 2015 at 21:31

1 Answer 1

1

Converting between JSON and XML contains example named Attribute to Force a JSON Array which says that you have to define a JSON namespace

xmlns:json='http://james.newtonking.com/projects/json'

in the XML's root element and add an attribute

json:Array='true'

to the element you wish to be converted into array (<category/> in your case).

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.