XmlDocument - 从字符串加载?

时间:2011-02-08 04:46:36

标签: c# asp.net xml json

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);
}

相反,如果从文件加载xml,我该如何从字符串加载?

1 个答案:

答案 0 :(得分:195)

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

str是您的XML字符串。有关详细信息,请参阅MSDN article