Reputation: 2838
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.
Upvotes: 1
Views: 252
Reputation: 20044
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);
}
Upvotes: 5