Whats the difference between loading an XML into a string or from a file stream?

Recommended Answers

All 3 Replies

I'm not sure what you're after. You can load XML in from a file stream. You can put XML into a string. You can read XML into a string from a file stream. Perhaps some more detail about what you're trying to accomplish will help us in guiding you to a solution.

Well I just wanted to no the difference between going

XmlDocument doc = new XmlDoucment();
doc.load(FileLocation)

or adding a FileStream or StreamReader and loading the doc from stream.

but I actually have another question. I have used this code to load all usernames from my xml file into a listbox.

    private void frmUserInfo_Load(object sender, EventArgs e)
    {
        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);
        }

    }

My xml file is something like

[UserAccounts]
[Account Username="Mastermosley"]
[Password]Password[/Password]
[Comment]Comments about Mastermosley[/Comment]
[/Account]
[/UserAccounts]

Now that all usernames are loaded into the list box when I click on a Username from the Listbox i want it to load the data: Password, and Comment from the XML file. How do I do this, I got something like:

    private void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        StreamReader stream = new StreamReader(XmlDoc);
        doc.Load(stream);
        //I Have no idea :(
    }

thanks

If you have a very large bunch of XML data, a string datatype isn't practical-- that string could potentially load hundreds of MB of data, risking a MemoryException being thrown if too much is loaded!

A stream is useful in many situations. I wrote a program that reads XML directly from a web service. The WebClient API returns that data as a stream, so that's how I read it in. Also, if you're doing an XMLReader instead of an XMLDocument, a stream is better suited, since you're going to be doing sequential reads from the XML stream, anyways. If you DO happen to read a large document, you minimize the chances of running out of memory. (XmlDocuments can still do this, though, so if you know you're going to be working with large documents, stick with the XmlReader).

Hope that helps! The big advantage to using streams is when you're working in a networked environment, like I mentioned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.