简体   繁体   中英

How to load and read XML documents

I'm using NET 2.0 WinForms for my C# application. Previously I used .NET 4.0 and used the following code to read an XML document:

XDocument doc = XDocument.Load(spath);
foreach (XElement xe in doc.Elements("Snippets").Elements("Snippet"))
{
    string sName = (string)xe.Attribute("name");
    string sCode = xe.Element("SnippetCode").Value;
    listBox1.Items.Add(snippetName);
    snippets.Add(sCode);
}

However, I don't know how to get the attribute and element value with .NET 2.0. Can anyone help me? I know I have to use XMLDocument but I don't know anything beyond loading an XML document in that.

Untested code, but I think you get the idea:

        XmlDocument doc = new XmlDocument();
        doc.Load(spath);
        foreach (XmlElement xe in doc.DocumentElement.SelectNodes("/Snippets/Snippet"))
        {
            string sName = xe.Attributes["name"].Value;
            string sCode = xe.SelectSingleNode("/SnippetCode").InnerText;
            listBox1.Items.Add(snippetName);
            snippets.Add(sCode);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM