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 8166925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:03:03+00:00 2026-06-06T20:03:03+00:00

So, essentially, I’m running into an interesting issue where, when the call to the

  • 0

So, essentially, I’m running into an interesting issue where, when the call to the “CreateXML()” function in the following code is made, an xelement is created as intended, but then, when I attempt to add it to a collection of xeleents, instead of continuing the foreach loop from which the call to “CreateXML()” originated, the foreach loop is broken out of, and a call is made to “WriteXML()“. Additionally, though an XElement is created and populated, it is not added to the List. [for clarification, the foreach loops I am referring to live in the “ParseDoc()” method]

    private List<XElement> _xelemlist;
    private void WriteXml()
    {
        XElement head = new XElement("header", new XAttribute("headerattributename", "attribute"));
        foreach (XElement xelem in _xelemlist)
        {
            head.Add(xelem);
        }
        XDocument doc = new XDocument();
        doc.Add(head);

    }
    private void CreateXML(string attname, string att)
    {
        XElement xelem = new XElement("name", new XElement("child", new XAttribute(attname, att), segment));
        _xelemlist.Add(xelem);
    }
    private void ExtractSegment(HtmlNode node)
    {
        HtmlAttribute[] segatts = node.Attributes.ToArray();
        string attname = segatts[0].Value.ToString();
        string att = node.InnerText.ToString();
        CreateXML(attname, att);
    }
    private HtmlDocument ParseDoc(HtmlDocument document)
    {
        try
        {
            HtmlNode root = document.DocumentNode.FirstChild;
            foreach (HtmlNode childnode1 in root.SelectNodes(".//child1"))
            {
                foreach (HtmlNode childnode2 in node.SelectNodes(".//child2"))
                {
                    ExtractSegment(childnode2);
                }
            }
        }
        catch (Exception e) { }
        WriteXml();
        return document;
    }

When I comment out the “List.Add()” in “CreateXML()” and step through the code, the foreach loop is not broken out of after the first iteration, and the code works properly.

I have no idea what I’m doing wrong (And yes, the code is instantiated by a public member, don’t worry: I am only posting the relevant internal methods to my problem)… if anyone has come across this sort of behavior before, I would really appreciate a push in the right direction to attempt to correct it… Sepcifically: is the problem just poor coding, or is this behavior a result of a property of one of the methods/libraries I am using?

One Caveat: I know that I am using HTMLAgilityPack to parse a file and extract information, but a requirement on this code forces me to use XDocument to write said information… don’t ask me why.

  • 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-06-06T20:03:05+00:00Added an answer on June 6, 2026 at 8:03 pm

    I have no idea what I’m doing wrong

    This, for starters:

    catch (Exception e) { }
    

    That’s stopping you from seeing what on earth’s going on. I strongly suspect you’ve got a NullReferenceException due to _xelemlist being null, but that’s a secondary problem. The main problem is that by pretending everything’s fine whatever happens, with no logging whatsoever, the only way of getting anywhere is by debugging, and that’s an awful experience when you don’t need to go through it.

    It’s extremely rarely a good idea catch exceptions and swallow them without any logging at all. It’s almost never a good idea to do that with Exception.

    Whenever you have a problem which is difficult to diagnose, improve your diagnostic capabilities first. That way, when you next run into a problem, it’ll be easier to diagnose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Essentially what I'm looking for is a function with the following prototype: string getln(istream);
Essentially, I'm looking for a 1D bar-code scanner that I can program, either through
Essentially, is there any way to make this code legal? main = print .
Essentially I have a method of a class called killProgram, which is intended to
Essentially I'm wondering if the following can be done in Ruby. So for example:
Essentially, what I'm looking for is a function that would allow me to do
Essentially I'm trying to combine the two resource declarations into one command. resource :documents,
Essentially I am looking for a PHP function or equation that will output the
Essentially, I'm grabbing source code which contains entities such as &#38; and &#62; which
Essentially, I have to get a flat file into a database. The flat files

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.