Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:20+00:00 2026-05-23T14:12:20+00:00

Here is a portion of the XML file I’m reading: <?xml version=1.0?> <movie xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

  • 0

Here is a portion of the XML file I’m reading:

<?xml version="1.0"?>
<movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ThumbGen="1">
  <hasrighttoleftdirection>false</hasrighttoleftdirection>
  <title>A Nightmare on Elm Street</title>
  <originaltitle>A Nightmare on Elm Street</originaltitle>
  <year>1984</year>
  <plot>Years after being burned alive by a mob of angry parents, child murderer Freddy Krueger returns to haunt the dreams -- and reality -- of local teenagers. As the town's teens begin dropping like flies, Nancy and her boyfriend, Glen, devise a plan to lure the monster out of the realm of nightmares and into the real world.</plot>
  <tagline>A scream that wakes you up, might be your own...</tagline>
  <metascore>78</metascore>
  <trailer>http://www.youtube.com/watch?v=996</trailer>
  <rating>8.6</rating>
  <episodes />
  <episodesnames />
  <writers />
  <gueststars />
  <id>tt0087800</id>
  <releasedate>11.09.1984</releasedate>
  <actor>
    <name>Robert Englund</name>
    <name>Heather Langenkamp</name>
    <name>Johnny Depp</name>
    <name>Ronee Blakley</name>
    <name>John Saxon</name>
    <name>Amanda Wyss</name>
    <name>Jsu Garcia</name>
    <name>Charles Fleischer</name>
    <name>Joseph Whipp</name>
    <name>Lin Shaye</name>
    <name>Joe Unger</name>
    <name>Mimi Craven</name>
    <name>David Andrews</name>
  </actor>
  <genre>
    <name>Horror</name>
    <name>Comedy</name>
  </genre>
  <director>
    <name>Wes Craven</name>
  </director>
  <runtime>91</runtime>
  <certification>R</certification>
  <studio>
    <name>New Line Cinema</name>
  </studio>
  <country>
    <name>United States of America</name>
  </country>
  ...
  ...
  ...
</movie>

Problem I’m running into is when I check for MPAA, if it doesn’t exist, then it runs to the end of the file, and I get stuck there. Not all the movies will have the MPAA, and for some reason, the XML doesn’t include an empty element in that case.

I need to figure out how I can test for it, and not lose the position, or how I can reset the position of the reader back to the top.

I tried reader.ResetState(), but I get “Root element is missing” error.

THEN, when I’m done with the file, I can’t figure out how to dispose of it so I can move to the next file in the list.

Yes, I’m a mess.

I’ll admit I’m new to XML. Hopefully you can get the idea of what’s going on with the code below. I would greatly appreciate suggestions on better/alternative ways to process these XML file. I have about 2000 of them, and they average 360 lines (15KB), but a few are 500 lines (50KB).

public static void ProcessMovies(string wPath, string cPath, string iPath)
{
    int lineID = 0;
    string strMovie = null;
    string strTitle = null;
    string strYear = null;
    string strPlot = null;
    string strRating = null;
    string strMPAA = null;
    string strCertification = null;
    string strGenre = null;

    // initiates streamwriter for output file
    FileInfo fi = new FileInfo(cPath + Path.DirectorySeparatorChar + "catalog.html");
    StreamWriter catalog = fi.AppendText();

    // pulls list of file and sorts them alphabetically
    // TODO: do "library sort" that ignores The, A at beginning of title
    string[] fns = Directory.GetFiles(wPath, "*.nfo");
    var sort = from fn in fns
               orderby new FileInfo(fn).Name ascending
               select fn;

    foreach (string n in sort)
    {
        if (lineID == 0)
            catalog.WriteLine("         <tr id=\"odd\">");
        else
            catalog.WriteLine("         <tr id=\"even\">");
        Console.WriteLine("Processing: " + n);

        XmlTextReader reader = new XmlTextReader(n);

            reader.ReadToFollowing("title");
            strTitle = reader.ReadElementContentAsString();

            reader.ReadToFollowing("year");
            strYear = reader.ReadElementContentAsString();

            reader.ReadToFollowing("plot");
            strPlot = reader.ReadElementContentAsString();

            reader.ReadToFollowing("rating");
            strRating = reader.ReadElementContentAsString();

                if (reader.ReadToFollowing("mpaa"))
                    strMPAA = reader.ReadElementContentAsString();
                else
                    strMPAA="UNKNOWN";

            // ugly code to try to read multiple embedded <name> elements within <genre>
            // NOTE: Possible only 1 genre
            reader.ResetState();
            reader.ReadToFollowing("genre");
            reader.Read();
            while ((reader.Name != "genre"))
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Text)
                    strGenre += reader.Value + ", ";
            }
            strGenre = strGenre.Substring(0, strGenre.Length - 2);

            reader.ReadToFollowing("certification");
            strCertification = reader.ReadElementContentAsString();

            reader.Close();

        strMovie =  "               <td>\r\n" + "                   <img src=\"" + JPG_FILE_NAME + "\" width=\"75\" height=\"110\">\r\n" + "                </td>\r\n" + "              <td>\r\n" + "                   <div id=\"title\">" + strTitle + "</div>" + "                   <div id=\"mpaa\">" + strMPAA + "</div>" + "                 <div id=\"genre\">" + strGenre + "</div>" + "                   <div id=\"plot\">" + strPlot + "</div>" + "             </td>" + "          </tr>";
        catalog.WriteLine(strMovie);

    }
    catalog.Close();
}


******************** EDIT ********************

Okay, I edited the code that processes the XML to the following per Henk’s suggestion:

var doc = XDocument.Load(n);  // takes care of all Open/Close issues
strTitle = doc.Root.Element("title") == null ? "" : doc.Root.Element("title").Value;
                strYear = doc.Root.Element("year") == null ? "" : doc.Root.Element("year").Value;
                strPlot = doc.Root.Element("plot") == null ? "" : doc.Root.Element("plot").Value;
                strRating = doc.Root.Element("rating") == null ? "" : doc.Root.Element("rating").Value;
                strMPAA = doc.Root.Element("mpaa") == null ? "" : doc.Root.Element("mpaa").Value;
                strCertification = doc.Root.Element("certification") == null ? "" : doc.Root.Element("certification").Value;

It works great, thank you very much!!!

Now for the last bit, how can I get the Genres from the genre name using this method? I can’t search for the name element since it is used in various elements. I wasn’t sure if I could work with:

doc.Root.Element("genre").ElementsAfterSelf("name");

Wasn’t clear on what that returns, or how it would handle multiple “names”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:12:21+00:00Added an answer on May 23, 2026 at 2:12 pm

    Unless your data is >> 100 MB, read it into a XDocument or XmlDocument.

    With XmlTextReader, you cannot search for optional elements. You can only retrieve and store each element as it comes along, and then ‘search’ for your elements in your own data structures.

    Roughly, using Sytem.Xml.Linq

     var doc = XDocument.Load(fileName);  // takes care of all Open/Close issues
     string title = doc.Element("title").Value;
     string mpaa = doc.Element("title") == null ? "" : doc.Element("mpaa").Value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a portion of the XML file I'm reading: <?xml version=1.0?> <movie xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
There's a code example demonstrating feComposite to be found here: http://www.w3.org/TR/SVG/filters.html#feCompositeElement But the example
Here is a portion of my struts.xml: <constant name=struts.custom.i18n.resources value=global /> and <action name=picture_save
Here is a portion of my app.yaml file: handlers: - url: /remote_api script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
I'm trying to following apache's ANT tutorial: http://ant.apache.org/manual/index.html I'm currently at the JUnit portion,
I am working with this XSD file. The portion of the XML that is
Here is a portion of my code: var styles:String = .keyword{color: #ff0000;} .comment{color: #00ff00;};
I'm trying to extract a portion of html between 2 comments. here is the
I'm using a recipe from the iOS Recipes PragProg book, here's the original portion
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.