Results 1 to 8 of 8

Thread: Reading an XML file with multiple root elements.

  1. #1

    Thread Starter
    Hyperactive Member Dodger's Avatar
    Join Date
    Mar 2001
    Posts
    433

    Arrow Reading an XML file with multiple root elements.

    Hey All,

    Using DataSet.ReadXML or the XMLTextReader function brings up an exception.

    Basically it says that there is more than one root element in my XML file, my files may or may not have this, is there anyway of ignoring this exception and reading the rest of the file?

    TIA.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I thought that a 'well-formed' XML document can only have one root. If that is truly the case, then the dataset will probably always give you an error because the document has to be well formed in order for it to read it.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    The statement about well formed xml is this:
    There is exactly one element, called the root, or document element, no part of which appears in the content of any other element. For all other elements, if the start-tag is in the content of another element, the end-tag is in the content of the same element. More simply stated, the elements, delimited by start- and end-tags, nest properly within each other.
    I got that from the specification pdf here:
    http://www.w3.org/TR/1998/REC-xml-19980210.pdf

  4. #4

    Thread Starter
    Hyperactive Member Dodger's Avatar
    Join Date
    Mar 2001
    Posts
    433
    They aren't my XML files, therefore I have no control over their content.

    There must be a way around it...surely.

    I REALLY don't want to have to use standard I/O.

    Any Ideas?

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I'll whack on it, could you please post some sample xml that is giving you problems?

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    A work around may be to upon it as text and add a root element in then save it as a temp file and open the temp file as xml. Just insert <root> before everything else or after any processing information and add </root> to the very end after everything. Then you have a well formed xml document.

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by Edneeis
    A work around may be to upon it as text and add a root element in then save it as a temp file and open the temp file as xml. Just insert <root> before everything else or after any processing information and add </root> to the very end after everything. Then you have a well formed xml document.
    That is a good idea... I didn't even think of that...lol.

    You could use a string builder object, then add the parts together real quick, and maybe use the string itself for the ReadXml method.

  8. #8

    Thread Starter
    Hyperactive Member Dodger's Avatar
    Join Date
    Mar 2001
    Posts
    433
    Slow_Learner,

    Sure...

    Main File:
    <root>

    <an_element1 anattribute="0">
    ElText1
    </an_element1>
    <an_element2 anattribute="0">
    ElText2
    </an_element2>

    <include file="example1.xml"/>

    </root>

    Example1.xml:
    <ex>
    <hello>
    Goodbye
    </hello>
    </ex>
    <ex> - Duplicate root element
    <hello>
    Hi
    </hello>
    </ex>

    As you can see, there is a main file, and the main file includes other files. The files it includes are obviously not formatted correctly.

    I could probably figure out a way to merge all the files into the main file, but I'd need to move them back out into individual files after editing is complete.

    Edneeis, I thought of that too, but I'll look at it as a last resort.

    Any more ideas?

    Thanks guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width