XmlDocument - 如何从字符串加载?

96
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,我该如何从字符串中加载它?


3
请查阅XmlDocument,您很快就能理解它。 - John Saunders
1
LoadXml() - http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspxLoadXml() - http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx - Dan Atkinson
1个回答

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

其中str是您的XML字符串。有关更多信息,请参见MSDN文章


新的XmlDocument() { InnerXml = str }怎么样? - mko
我认为LoadXml()不仅仅是设置InnerXml。https://referencesource.microsoft.com/#System.Xml/System/Xml/Dom/XmlDocument.cs,774e340cfa235dc6 - vinibrsl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接