loading xml data into strings

This is a discussion on loading xml data into strings within the C# Programming forums, part of the General Programming Boards category; I have an xml file with: Code: <UserAccounts> <Account username="username"> <password="password"/> <banned="no"/> <ipbann="no"/> <inactive="no"/> </Account> </UserAccounts> I want to load ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    23

    loading xml data into strings

    I have an xml file with:

    Code:
    <UserAccounts>
    <Account username="username">
    <password="password"/>
    <banned="no"/>
    <ipbann="no"/>
    <inactive="no"/>
    </Account>
    </UserAccounts>
    I want to load each one of the elements into separate strings at runtime. I've been messing around with XMLTextReader for a few hours and am starting to wonder if thats what I use. To be honest I've never incorporated Xml into any programming projects at all so any help or ideas would definitely be appreciated thanks.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    This can be done very easily using the System.Xml.Linq namespace.

    Use XElement.

    For example:
    Code:
    XElement root = XElement.Load("PathToFile");
    
    Console.WriteLine(root.Descendants("password").Value);
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Ok I'm testing this right now, I would I program a loop to check for all <Account username="
    and load them into a textbox or listbox.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    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?

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Mastermosley View Post
    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?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    23

    Exclamation

    Well you can do this:

    XmlDocument doc = new XmlDocument();
    doc.Load(PathToXML)

    or I can

    XmlDocument doc = new XmlDocument();
    FileStream fs = new FileStream(PathToXML, FileMode.Open, FileAccess.Write)
    doc.Load(fs);

    What would be the difference?

    Also, I want to no the advantages and disadvantages of using: XmlDocument , XPathDocument, XmlReader and XmlWriter classes. My application is going to be using a few large xml databases and I want reading and writing to be fast. All I know about them is XmlDocument loads the entire xml file into memory while the others dont?

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Mastermosley View Post
    Well you can do this:

    XmlDocument doc = new XmlDocument();
    doc.Load(PathToXML)

    or I can

    XmlDocument doc = new XmlDocument();
    FileStream fs = new FileStream(PathToXML, FileMode.Open, FileAccess.Write)
    doc.Load(fs);

    What would be the difference?

    Also, I want to no the advantages and disadvantages of using: XmlDocument , XPathDocument, XmlReader and XmlWriter classes. My application is going to be using a few large xml databases and I want reading and writing to be fast. All I know about them is XmlDocument loads the entire xml file into memory while the others dont?
    There is no real difference - it all depends on your program. It simply has many different constructors for the programmer's convenience, as he might have the path, or just a filestream in which the file is already opened.

    I'm not sure about the performance of those classes - sounds like something you should ask google, as someone has probably asked this question before.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Loading a struct with data from a file
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 10-28-2001, 07:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21