locked
Read Json file and xml file RRS feed

  • Question

  • User-148788041 posted

    Hi,

    I have the below files . i want to read independent tag names .Also validate if it is Valid Json file or valid XML.

    Json
    {
    Id=1,
    Name="Sam"
    LastName="K"
    Marks= 90
    }
    
    
    XML:
    <?xml>
    <id><1</id>
    <Name>Sam</Name>
    <LastName>K</LastName>
    <Marks>90</Marks>
    

    Monday, September 24, 2018 4:58 PM

All replies

  • User475983607 posted

    Can you explain why you do not know the type of file you are reading?  Generally a JSON file has a json extension and an XML file has an xml extension. 

    If the file extension is unknown the first line of an XML file has <?xml and the first character is a "<".

    Is there anyway you can post your code and explain the issue you are having?  Secondly, the community has provided many examples of deserializing JSON and XML in your previous threads on the same subject. Can you explain why you are having issues at this time?

    Monday, September 24, 2018 5:40 PM
  • User-893317190 posted

    Hi Guhananth,

    If you want to validate a json or xml.

    You could  try to deserialize the json file and use try catch to see if it is valid.

    About xml , you could load the xml file to see if it is valid.

     System.Web.Script.Serialization.JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                try
                {
                    javaScriptSerializer.DeserializeObject(File.ReadAllText(Server.MapPath("/Format/json.json")));
                }
                catch (Exception ex)
                {
    
                    Response.Write(ex.ToString());
                }
    
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(Server.MapPath("/Format/XMLFile1.xml"));
                }
                catch (Exception ex)
                {
    
                    Response.Write(ex);
                }
               

    You could also use visual studio to create a json file and if it is not valid ,visual studio will give a prompt.

    xml is the same.

    I don't know clear about what  you mean by " i want to read independent tag names",could you explain it?

    Best regards,

    Ackerly Xu

    Tuesday, September 25, 2018 5:31 AM
  • User-647250237 posted

    Please <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="9" data-gr-id="9">refer</g> this link.

    http://www2.hawaii.edu/~takebaya/ics111/xml_json/xml_json.html

    Friday, October 19, 2018 12:05 PM