XmlDocument的 - 从字符串负荷? [英] XmlDocument - load from string?

查看:122
本文介绍了XmlDocument的 - 从字符串负荷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    XmlDocument的DOC =新的XmlDocument();
    尝试
    {
        字符串路径=使用Server.Mappath(。);
        doc.Load(路径+whatever.xml);
    }
    赶上(异常前)
    {
        lblError.Text = ex.ToString();
        返回;
    }    //将XML转换为JSON字符串
    JSON字符串= XmlToJSON(DOC);    //替换\\以\\\\因为字符串是德codeD两次
    JSON = JSON.Replace(@\\,@\\\\);    //将code到页面的结束进程JSON
    ClientScriptManager CS = Page.ClientScript;
    cs.RegisterStartupScript(的GetType(),SpaceJSON,space_processJSON('+ JSON +');,真);
}

相反,如果从文件加载XML的,我怎么从字符串加载它?


解决方案

  XmlDocument的DOC =新的XmlDocument();
doc.LoadXml(STR);

其中, STR 是XML字符串。请参阅 MSDN文章更多信息。

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?

解决方案

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

Where str is your XML string. See the MSDN article for more info.

这篇关于XmlDocument的 - 从字符串负荷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆