0

Hello,

I'm new to XML and working on a searchable movie reader application which will read an XML file called MovieData.xml. The XML file contains the following nodes:

Example,

<movie>
    <title>Silence of the Lambs</title>
    <disc>5</disc>
    <year>1991</year>
    <director>Unspecified</director>
    <cast>Unspecified</cast>
    <genre>Horror</genre>
    <riptype>DVDRip</riptype>
    <codecvideo>DIVX</codecvideo>
    <codecaudio>MP3</codecaudio>
    <imdbrating>8.7</imdbrating>
    <comments>Some Comments Here</comments>

 </movie>

To read the XML data i'm using the following (simplified example).

// Example 1

static string xmlDocumentPath = @"MovieDat.xml";

static XmlDocument movieXmlDoc = new XmlDocument();

movieXmlDoc.Load(xmlDocumentPath); // Load the XML document

//

Then to read through the XML file I use,

//

string XmlPath = ("//class_movielist/movie/" + category); // Path to the selected node         

 XmlNodeList nodes = movieXmlDoc.SelectNodes(XmlPath);

 foreach (XmlNode node in nodes)
{

  // Code here

}

//

I hope i'm making sense so far.

The app works well but i'm wondering if I should add the 'XmlTextReader' to 'XmlDocument' object.

ie,

// Example 2

static string xmlDocumentPath = @"MovieDat.xml";

static XmlDocument movieXmlDoc = new XmlDocument( );

static XmlTextReader xmlTextRead = new XmlTextReader(xmlDocumentPath); // ?

movieXmlDoc.Load(xmlTextRead); // Load the XML document

//

Both examples work fine but what are the advantages/disadvantages between the two examples ?

I would be greatful for an explaination of the differences between the two.

Many Thanks

Steve H.

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by apegram
0

Overall, there will be little difference. When you use the Load overload that accepts a string path for the file, it is going to create a reader from that file and then use it to load the XmlDocument. When you pass in the reader, you've obviously taken care of that step yourself. With that said, if the object is going to do it for you anyway, why waste your own time? Pass in the string and skip the (manual) step of creating the reader. You'll save time and lines of code.


*On that note, the explicit usage of XmlTextReader has been discouraged in favor of using XmlReader.Create since .NET 2.0. If you need a reader, obtain one using that method.

XmlReader reader = XmlReader.Create(path);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.