Thursday, February 9, 2017

The ' ' character, hexadecimal value 0x20, cannot be included in a name.




Introduction

I want to convert Json file to xml file. Many code available on internet you can applied easily and It is working fine. but sometime we got an error. 

"The ' ' character, hexadecimal value 0x20, cannot be included in a name."


Solution

Sol 1- jsonStr.Replace(“ “,””);
              
With this solution, String does not contain any space.

Sol 2-
var objJson = JArray.Parse(jsonString);
 foreach (var fix in (from property in objJson.Descendants().OfType<JProperty>()
         let newName = XmlConvert.EncodeLocalName(property.Name.Replace(" """))
         where newName != property.Name
               select new { Old = property, New = new JProperty(newName, property.Value) })          .ToList())
         {
            fix.Old.Replace(fix.New);
         }

                XmlDocument xdoc = JsonConvert.DeserializeXmlNode("{\"root\":" + objJson.ToString() + "}""root");