2011-02-08 10 views
74
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); 
} 

En lugar de cargar el xml de un archivo, ¿cómo lo cargo desde una cadena?XmlDocument - cargar desde cadena?

+3

Busque la clase ['XmlDocument'] (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx). Lo descubrirás muy rápido. –

+0

'LoadXml()' - http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx –

Respuesta

175
XmlDocument doc = new XmlDocument(); 
doc.LoadXml(str); 

Donde str es su cadena XML. Vea el MSDN article para más información.

+0

Simple. Cuando sabes cómo. –

+1

LoadXml. ¿Quién lo habría dicho? –

+0

¿Qué hay de nuevo XmlDocument() {InnerXml = str}? – mko

Cuestiones relacionadas