Force a write

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MAF

    #1

    Force a write

    I am trying to save an XMLDocument and I occasionally get a sharing
    violation. Can I force the write in this case?

    System.Xml.XmlD ocument XMLDoc = new System.Xml.XmlD ocument();

    //Load file

    //Do something

    XMLDoc.Save("c: \test.xml");


  • MAF

    #2
    Re: Force a write

    Sorry my mistake I forgot to close the reader. This code fixed the problem

    System.IO.FileS tream fin ;

    fin = new System.IO.FileS tream(@XMLDataF ile, System.IO.FileM ode.Open,
    System.IO.FileA ccess.Read, System.IO.FileS hare.ReadWrite) ;

    XMLDoc.Load(new System.IO.Strea mReader(fin)) ;

    fin.Close();



    "MAF" <mfraser@henwoo denergy.com> wrote in message
    news:#jyfjCKfDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
    > I am trying to save an XMLDocument and I occasionally get a sharing
    > violation. Can I force the write in this case?
    >
    > System.Xml.XmlD ocument XMLDoc = new System.Xml.XmlD ocument();
    >
    > //Load file
    >
    > //Do something
    >
    > XMLDoc.Save("c: \test.xml");
    >
    >[/color]


    Comment

    • Jon Skeet

      #3
      Re: Force a write

      MAF <mfraser@henwoo denergy.com> wrote:[color=blue]
      > Sorry my mistake I forgot to close the reader. This code fixed the problem
      >
      > System.IO.FileS tream fin ;
      >
      > fin = new System.IO.FileS tream(@XMLDataF ile, System.IO.FileM ode.Open,
      > System.IO.FileA ccess.Read, System.IO.FileS hare.ReadWrite) ;
      >
      > XMLDoc.Load(new System.IO.Strea mReader(fin)) ;
      >
      > fin.Close();[/color]

      I would recommend using the using constructor for this:

      using (FileStream fin = new FileStream (XMLDataFile,
      FileMode.Open,
      FileAccess.Read ,
      FileShare.ReadW rite))
      {
      using (StreamReader reader = new StreamReader (fin))
      {
      XMLDoc.Load (reader);
      }
      }

      While you don't *need* to close the reader, I generally think it's
      better to dispose of everything that can be disposed.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Jon Skeet

        #4
        Re: Force a write

        Jon Skeet <skeet@pobox.co m> wrote:[color=blue]
        > I would recommend using the using constructor for this:[/color]

        <snip>

        Sorry, that should have been "the using construct" not "the using
        constructor". Darned muscle memory :(

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...