Why is my XML file being written corrupt?

Discussion in 'ASP .Net' started by darrel, Apr 6, 2006.

  1. darrel

    darrel Guest

    In our CMS, every time a page is updated, we spit out a new XML file.

    This file is written via a function that queries the Database using a
    recursive function, grabs the data, and writes out a file using
    xmltextwriter:

    Dim fs As New
    System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath("/mjb05/xml/"
    & siteID & "/siteMenu.xml").ToString, IO.FileMode.Open, IO.FileAccess.Write,
    IO.FileShare.Read)
    objXMLWriter = New System.xml.XmlTextWriter(fs, Nothing)
    'objXMLWriter = New
    System.xml.XmlTextWriter(System.Web.HttpContext.Current.Server.MapPath("/mjb05/xml/"
    & siteID & "/siteMenu.xml").ToString, Nothing)
    objXMLWriter.Flush()
    objXMLWriter.Formatting = objXMLWriter.Formatting.Indented
    objXMLWriter.Indentation = 3
    objXMLWriter.WriteStartDocument()
    objXMLWriter.WriteComment("Created on " & Now())
    objXMLWriter.WriteStartElement("menuItems")
    createParentNode = False

    ....etc.

    This works 99% of the time. However, if I change a particular value in one
    of my table cells the writer still executes, but then writes out an invalid
    ending to the file.

    What is REALLY odd is that I can re-write the file a second time and it
    fixes itself.

    What is even odder (yes, it gets odder) is the way the file is corrupted.
    It's appending a weird bit of broken nodes to the end of the file.

    For instance, if my file SHOULD end like this:

    </endNode>
    </endAllItems>

    the corrupted file might look like this:

    </endNode>
    </endAllItems>
    </endNode>
    </endAllItems>

    or this:

    </endNode>
    </endAllItems>endNode>
    </endAllItems>

    or even just this:

    </endNode>
    </endAllItems>s>

    Anyone have any ideas what could cause this type of weird corruption when
    writing out an XML file?

    -Darrel
     
    darrel, Apr 6, 2006
    #1
    1. Advertising

  2. darrel

    darrel Guest

    > This works 99% of the time. However, if I change a particular value in one
    > of my table cells the writer still executes, but then writes out an
    > invalid ending to the file.


    Some more info:

    I've looked at the data in the db when writing:

    data record 1 = writes XML file
    data record 2 = writex XML file
    return to data record 1 = writes CORRUPTED file
    write again with data record 1 = writes XML file

    In all instance, the same variables are passed to the function that writes
    out the xml file.

    In addition, the corruption is random but consistent (make sense? ;o)

    It's always one or two (possibly partial) repeats of the last node and the
    parent node closing tags at the end of the file.

    -Darrel
     
    darrel, Apr 6, 2006
    #2
    1. Advertising

  3. darrel

    Ray Booysen Guest

    darrel wrote:
    >> This works 99% of the time. However, if I change a particular value in one
    >> of my table cells the writer still executes, but then writes out an
    >> invalid ending to the file.

    >
    > Some more info:
    >
    > I've looked at the data in the db when writing:
    >
    > data record 1 = writes XML file
    > data record 2 = writex XML file
    > return to data record 1 = writes CORRUPTED file
    > write again with data record 1 = writes XML file
    >
    > In all instance, the same variables are passed to the function that writes
    > out the xml file.
    >
    > In addition, the corruption is random but consistent (make sense? ;o)
    >
    > It's always one or two (possibly partial) repeats of the last node and the
    > parent node closing tags at the end of the file.
    >
    > -Darrel
    >
    >

    To me it looks like a logic fault in your recursive functions. It might
    be there is a fault in how elements are being closed and taken care of.
    Step through the code for a small XML file and see where the problem is.
     
    Ray Booysen, Apr 6, 2006
    #3
  4. darrel

    Darrel Guest

    > To me it looks like a logic fault in your recursive functions. It might
    > be there is a fault in how elements are being closed and taken care of.
    > Step through the code for a small XML file and see where the problem is.


    I'll give it a shot. However, my function is using xmlwriter to write out
    individual nodes so even if it is a logic error, it's hard to figure out why
    it would be writing half-tags.

    I'll report back!

    -Darrel
     
    Darrel, Apr 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    563
    reaway lee
    Aug 24, 2003
  2. Replies:
    7
    Views:
    663
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,984
    Smokey Grindel
    Dec 2, 2006
  4. KaiWen
    Replies:
    102
    Views:
    2,735
    Jorgen Grahn
    Sep 15, 2011
  5. B
    Replies:
    9
    Views:
    313
Loading...

Share This Page