Originally Posted by
Mastermosley
Your way never really worked, the .value didnt exist so I did it like this to load the data into a list.
Code:
XmlDocument doc = new XmlDocument();
StreamReader stream = new StreamReader(XmlDoc);
doc.Load(stream);
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
lstUsers.Items.Add(node.Attributes["username"].Value);
}
stream.Close();
That succesfully loads all usernames into the listbox. If you look above my xml file contains boolean values and password in each username tag. I have a textbox on the form that I want to load with the data so what I really want to do is load all the childnodes from the username attribute into a string array. Then when the user clicks on one of the username values in the listbox it will load the information into the textbox.
Also just a question I noticed that you can load the xml file into a string or a file stream, what is the difference?
I'm sorry - I gave you incorrect information.
Since it's LINQ, it seems to return an IEnumerable collection of descendants matching "password" in the code supplied above. This code does what's intended:
Code:
XElement root = XElement.Load(@"PathHere");
XElement passwordElement = root.Descendants("password").Single();
Console.WriteLine(passwordElement.Name + " = " + passwordElement.Value);
You can also use the Elements member function to print all child elements that are direct children(So to speak), as opposed to children of children.
Here's an example of a function that prints all child elements in a tree structure recursively. The PrintTabs is merely for the aesthetics.
Code:
class Program
{
static void Main(string[] args)
{
XElement root = XElement.Load(@"PathHere");
PrintChildElements(root, 0);
Console.ReadKey();
}
public static void PrintChildElements(XElement element, int tabCount)
{
foreach (var child in element.Elements())
{
PrintTabs(tabCount);
Console.WriteLine(child.Name);
if (child.HasElements)
PrintChildElements(child, tabCount + 1);
}
}
public static void PrintTabs(int tabCount)
{
for (int i = 0; i < tabCount; i++)
{
Console.Write("\t");
}
}
}
I'm not sure I understood what you meant by your last question - care to elaborate further?