2

I'm using Json.NET in a .NET 4.0 application in order to convert a JSON RESTful response into XML. I am running into issues converting JSON into XML if a JSON child key has a space.

So far, I am able to convert most JSON responses.

Here are example responses along with the code which I am using to generate the XML.

{
   num_reviews: "2",
   page_id: "17816",
   merchant_id: 7165
}

And here is the response which is causing an error:

    [
    {
    headline: "ant bully",
    created_date: "2010/06/12",
    merchant_group_id: 10126,
    profile_id: 0,
    provider_id: 10000,
    locale: "en_US",
    helpful_score: 1314,
    locale_id: 1,
    variant: "",
    bottomline: "Yes",
    name: "Jessie",
    page_id: "17816",
    review_tags: [
    {
    Pros: [
    "Easy to Learn",
    "Engaging Story Line",
    "Graphics",
    "Good Audio",
    "Multiplayer",
    "Gameplay"
    ]
    },
    {
    Describe Yourself: [
    "Casual Gamer"
    ]
    },
    {
    Best Uses: [
    "Multiple Players"
    ]
    },
    {
    Primary use: [
    "Personal"
    ]
    }
    ],
    rating: 4,
    merchant_id: 7165,
    reviewer_type: "Verified Reviewer",
    comments: "fun to play"
    },
    {
    headline: "Ok game, but great price!",
    created_date: "2010/02/28",
    merchant_group_id: 10126,
    profile_id: 0,
    provider_id: 10000,
    locale: "en_US",
    helpful_score: 1918,
    locale_id: 1,
    variant: "",
    bottomline: "Yes",
    name: "Alleycatsandconmen",
    page_id: "17816",
    review_tags: [
    {
    Pros: [
    "Easy to Learn",
    "Engaging Story Line"
    ]
    },
    {
    Describe Yourself: [
    "Frequent Player"
    ]
    },
    {
    Primary use: [
    "Personal"
    ]
    },
    {
    Best Uses: [
    "Kids"
    ]
    }
    ],
    rating: 3,
    merchant_id: 7165,
    reviewer_type: "Verified Reviewer",
    comments: "This is a cute game for the kids and at a great price. Just don't expect a whole lot."
    }
    ]

So far, I have been considering on creating a mapping of the JSON data to a C# object and generating XML for that class. However, is there a way to keep this dynamic? Or is there a way to treat spaces as %20 encodings?

1
  • That's not a valid JSON string (or even javascript for that matter). If the property name requires spaces, it must be quoted. Either fix the response so it returns a valid string or write your own parser to parse it. Commented Jul 23, 2013 at 15:47

3 Answers 3

1

This question is same as how to validate JSON string before converting to XML in C#

If you have any further queries, please let me know.

Sign up to request clarification or add additional context in comments.

Comments

1

You can call XmlConvert.EncodeName, which will escape any invalid characters using _s.

For example, a space would become _x0020_.

1 Comment

While this is good, I have issues getting JsonConvert.DeserializeNode() to generate the correct XML. The reason being that the original source code does NOT try to handle spaces.
0

You cannot have an XMLElement Name with a space in it. You would need to replace the space with an Underscore or anyother element. If that is not feasible for you, try putting that value as an attribute for that Node. I hope this makes sense.

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.