locked
Root Element is Missing RRS feed

  • Question

  • User-73514677 posted

    Hi..

    In my web page, I have the following code.

    string TargetDir = Server.MapPath("").ToString() + \\FOLDER/EmailCamp.xml;

     XmlTextReader xmlreader = new XmlTextReader(TargetDir);

    while (xmlreader.Read())

    {

    }

    I am getting an exception as "Root Element is missing", in the while loop. This exception is caused if there are no elements in the xml.

    How to catch this exception?

     

    Friday, December 28, 2007 12:47 AM

Answers

  • User300685930 posted

    try

    {

    while (xmlreader.Read())

    {

    }

    }

    catch (Exception ee)

    {

    throw new ApplicationException(ee.ToString());

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 28, 2007 1:31 AM
  • User1167746849 posted

    The cause of the XmlException entitled Root element is missing means the XML document you're trying to load is not formatted properly, more exactly it's missing the root node.
    Each XML file must have a root element / node which encloses all the other elements.

    Also, a blank XML file will return the same Root elements is missing exception

    If possible you can post the xml file also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 28, 2007 3:32 AM
  • User372121194 posted

    Hi,

     Thank you very much for the reply.

    I do have a try and catch block, before doing the Read operation. Is it possible to check if there are atleast one node in the xml or if the xml file is empty and then call the xmlreader.Read()

    Thank you

    Hi,

    Based on my understanding, you want to know how to check if the xml file has root element. If I have misunderstood you, please feel free to let me know.

    As far as I know, we can read this file to string and then parse the string to check if this xml file has element. It is difficult to do this. I suggest you use try/catch block just following the member’s suggestion.

     

    Happy New Year!

    I hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 1, 2008 4:22 AM

All replies

  • User300685930 posted

    try

    {

    while (xmlreader.Read())

    {

    }

    }

    catch (Exception ee)

    {

    throw new ApplicationException(ee.ToString());

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 28, 2007 1:31 AM
  • User1167746849 posted

    The cause of the XmlException entitled Root element is missing means the XML document you're trying to load is not formatted properly, more exactly it's missing the root node.
    Each XML file must have a root element / node which encloses all the other elements.

    Also, a blank XML file will return the same Root elements is missing exception

    If possible you can post the xml file also.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 28, 2007 3:32 AM
  • User-73514677 posted

    Hi,

     Thank you very much for the reply.

    I do have a try and catch block, before doing the Read operation. Is it possible to check if there are atleast one node in the xml or if the xml file is empty and then call the xmlreader.Read()

    Thank you

    Friday, December 28, 2007 3:51 AM
  • User372121194 posted

    Hi,

     Thank you very much for the reply.

    I do have a try and catch block, before doing the Read operation. Is it possible to check if there are atleast one node in the xml or if the xml file is empty and then call the xmlreader.Read()

    Thank you

    Hi,

    Based on my understanding, you want to know how to check if the xml file has root element. If I have misunderstood you, please feel free to let me know.

    As far as I know, we can read this file to string and then parse the string to check if this xml file has element. It is difficult to do this. I suggest you use try/catch block just following the member’s suggestion.

     

    Happy New Year!

    I hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 1, 2008 4:22 AM
  • User-73514677 posted

     Hi.,

     Yes, I would like to check if the xml file has root element. As you suggested, I will go on with the the try/catch block..

    Happ New Year...

    Wednesday, January 2, 2008 12:15 AM