protected void Page_Load(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); try { string path = Server.MapPath("."); doc.Load(path+"whatever.xml"); } catch (Exception ex) { lblError.Text = ex.ToString(); return; } // Convert XML to a JSON string string JSON = XmlToJSON(doc); // Replace \ with \\ because string is being decoded twice JSON = JSON.Replace(@"\", @"\\"); // Insert code to process JSON at end of page ClientScriptManager cs = Page.ClientScript; cs.RegisterStartupScript(GetType(), "SpaceJSON", "space_processJSON('" + JSON + "');", true); }
Instead if of loading the xml from a file, how do I load it from a string?
To load XML from a string To populate an XML literal such as an XElement or XDocument object from a string, you can use the Parse method. The following code example shows the use of the XDocument. Parse(String) method to populate an XDocument object with XML from a string.
Use XmlDocument. Load() method to load XML from your file. Then use XmlDocument. InnerXml property to get XML string.
XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.
In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.
XmlDocument doc = new XmlDocument(); doc.LoadXml(str);
Where str
is your XML string. See the MSDN article for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With