how to deserialize json into dynamic object in c# and update field

NALB 71 Reputation points
2023-04-03T11:59:17.7933333+00:00

Hello ,

I have a Json file that dynamically changes as the pages of the Json keep change their names so in order to Deserialize it I used this code

 var dynamicObject= await XMLManager.DeserializeJson<dynamic>(pathT)

and here is a sample of my output json

{"Row": {
  "version": "1.7.0",
  "pages": {
    "https://test:443": {
      "uuid": "",
      "notes": null
    },
   
  },
  
}}

my problem now is that I need to change it to XML to do this I use the below code


   var data = "{\"Row\": " + dynamicObject + "}";
 XmlDocument doc = JsonConvert.DeserializeXmlNode(data.ToString() , "root");

but after running that I keep getting that error

XMLSubdomainTakeOver.xml System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name.
   at System.Xml.XmlDocument.CheckName(String name)
   at System.Xml.XmlDocument.CreateElement(String prefix, String localName, String namespaceURI)
   at System.Xml.XmlDocument.CreateElement(String name)
   at Newtonsoft.Json.Converters.XmlDocumentWrapper.CreateElement(String elementName)

I don't know what is the problem but I have tried to remove the : and // from the pages node children in order to solve the problem as below

 foreach (var page in dynamicObject.pages)
                {
                    page.Name= Regex.Replace(page.Name, "://", "_");
                    page.Name = Regex.Replace(page.Name, ":", "_");
                }

But running this code catch the below exception

 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Property or indexer 'Newtonsoft.Json.Linq.JProperty.Name' cannot be assigned to -- it is read only      


Can you tell me what is my problem and how can I resolve that ?

Thank You

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-04-04T08:13:27.27+00:00

    @NALB, Welcome to Microsoft Q&A, based on my test, I reproduced the same problem with you. The first error due to the url in the xml can not be identified. The second error means that we could not change the json name programmatically. I suggest that you could get the name first and replace the string in the end. Here is a code example you could refer to.

                var json = File.ReadAllText("B:\\t.json");
                var jsonReturn = JsonConvert.DeserializeObject<dynamic>(json);
                string pagestr = string.Empty;
                string str = "";
                foreach (var item in jsonReturn.pages)
                {
                    pagestr += item.Name;
                    str = Regex.Replace(item.Name, "://", "_");
                    str = Regex.Replace(str, ":", "_");
                }
                json=json.Replace(pagestr,str);
                var data = "{\"Row\": " + json + "}";
                XmlDocument doc = JsonConvert.DeserializeXmlNode(data.ToString(), "root");
                Console.WriteLine( doc.InnerXml);
    
    

    Tested result: User's image

    Hope my advice could help you. Best Regards, Jack


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.