2

I have been trying out Azure's recently announced search service and am using JSON.Net to convert the resulting JSON output into XML. It normally works okay except when adding the highlight parameter I get a Cannot get an XML string value from token type 'StartObject'. Path 'value[0][email protected]' error message. The highlight parameter adds the @search.highlights which the parser doesn't seem to like. I also checked the JSON output at jsonvalidatortool.com which resulted as valid.

The JSON received from the Azure search service is as follows:

{
  "value":
    [
      {
        "@search.score": 1.2591839,
        "@search.highlights": 
          {
            "[email protected]": "#Collection(String)",
            "kbTitle": [
              "No Video / Blank Screen When Attempting to Stream from <em>Netflix</em>"
            ]
          },
        "kbID":"10",
        "kbTitle":"No Video / Blank Screen When Attempting to Stream from Netflix"
      }
    ]
}

Anyone else trying the new Azure search service with JSON.Net and experiencing this issue?

1 Answer 1

2

Unfortunately, this JSON, while valid, cannot be converted directly to XML by Json.Net for two reasons:

  1. When JSON.Net sees a JSON property name starting with @, it tries to convert it to an attribute in the XML. An XML attribute must be a simple value (string, integer, etc.); it cannot be a complex object. In the JSON, the value of the @search.highlights property is clearly a complex object, so it cannot be converted to an XML attribute. That is why you are receiving the error.

  2. XML tags names cannot contain an @. Assuming we were able to get past the first issue (e.g. by removing the leading @ from @search.highlights), another error will be thrown when trying to convert the [email protected] property into an XML tag due to the fact that it contains a @ in the middle.

A couple of possible solutions come to mind:

The quick-and-dirty approach would be to try to do a string replace on the JSON prior to converting it to XML. If the problem areas are just those two specific property names I highlighted, then you could target them specifically to remove or replace the @ characters with something more palatable to XML like an underscore _. If the JSON is more dynamic such that the problem property names are highly variable, then this approach is less likely to work correctly. You know your data better than I do-- I am not all that familiar with the Azure Search Service.

A more robust solution would be to deserialize the JSON to a JObject, then manually traverse the JObject and convert it into XML the way you need it. You can omit the parts you don't need, change property/tag names, etc. Of course, this assumes you are familiar with Json.Net's LINQ-to-JSON API (JObject, JTokens, JArrays, etc.) and are comfortable working with the classes in the System.Xml namespace in the .NET framework.

Another idea would be to create an intermediate model class, deserialize the JSON into that (so you can use [JsonProperty] attributes to deal with the funky property names), then convert that model into XML using an XmlSerializer.

Hope this helps.

1
  • 1
    you're awesome! Thanks to your thorough explanation and suggested solutions, I now have a good understanding of the issue and how to handle it.
    – Kevin
    Aug 28, 2014 at 17:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.