为什么XMLWriter的不关闭? [英] Why is the XMLWriter Not Closing?

查看:520
本文介绍了为什么XMLWriter的不关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与XMLWriter的不关闭一些麻烦。我可以成功地编写XML文件一次,但如果我尝试再次写入(覆盖)我得到异常:

 的进程无法访问该文件'somefile.xml',因为它正由另一个进程使用。

 暗淡设置=新XmlWriterSettings()
    settings.Indent = TRUE
    settings.IndentChars =
    settings.NewLineOnAttributes = TRUE

    尝试
    昏暗的作家作为的XmlWriter = XmlWriter.Create(System.IO.File.Create(somefile.xml))
        writer.WriteStartDocument(真)
        writer.WriteStartElement(根)
        对于rowCounter作为整数= ds.Tables(0).Rows.Count  -  1 0步骤-1
            writer.WriteStartElement(someelement)
            writer.WriteElementString(COL0,ds.Tables(0).Rows(rowCounter)(0)的ToString)
            writer.WriteElementString(col1中,ds.Tables(0).Rows(rowCounter)(1)的ToString)
            writer.WriteElementString(COL2,ds.Tables(0).Rows(rowCounter)(2)的ToString)
            writer.WriteElementString(COL3,ds.Tables(0).Rows(rowCounter)(3)的ToString)
            writer.WriteElementString(COL4,ds.Tables(0).Rows(rowCounter)(4)的ToString)
            writer.WriteElementString(COL5,ds.Tables(0).Rows(rowCounter)(5)的ToString)
            writer.WriteElementString(COL6,ds.Tables(0).Rows(rowCounter)(6)的ToString)
            writer.WriteElementString(COL7,ds.Tables(0).Rows(rowCounter)(7)的ToString)
            writer.WriteEndElement()
        下一个
        writer.WriteEndElement()
        writer.WriteEndDocument()
    抓住EX作为System.IO.IOException
        的MessageBox.show(ex.Message)
    最后
        writer.Flush()
        writer.Close()
    结束尝试
 

解决方案

你所缺少的,是 XmlWriterSettings 。您声明它,但你不使用它,当你不设置的CloseOutput 手工,默认为false,这意味着输出不关闭(在这种情况下,你的文件流)。

要拥有它的行为,你想的那样,改变你的code这样的方式:

 暗淡设置=新XmlWriterSettings()
settings.Indent = TRUE
settings.IndentChars =
settings.NewLineOnAttributes = TRUE
settings.CloseOutput =真正的'<<<<改变 '

使用作家作为的XmlWriter = XmlWriter.Create(System.IO.File.Create(somefile.xml),设置)
    '.... 等等'
结束使用
 

在万一你想知道如何真正在内部运作,这里的XmlEn codedRawTextWriterIndent,在方案中使用的内部的XmlWriter的关闭方法。

  //礼貌红门的反射
公众覆盖无效关闭()
{
    this.FlushBuffer();
    this.FlushEn codeR();
    this.writeToNull = TRUE;
    如果(this.stream!= NULL)
    {
        this.stream.Flush();
        如果(this.closeOutput)//该标志设置为settings.CloseOutput
        {
            this.stream.Close();
        }
        this.stream = NULL;
    }
    否则,如果(this.writer!= NULL)
    {
        this.writer.Flush();
        如果(this.closeOutput)
        {
            this.writer.Close();
        }
        this.writer = NULL;
    }
}
 

I am having some trouble with the XMLWriter not closing. I can successfully write the XML file once but if I try to write it again (overwrite) I get the exception:

"The process cannot access the file 'somefile.xml' because it is being used by another process."

    Dim settings = New XmlWriterSettings()
    settings.Indent = True
    settings.IndentChars = " "
    settings.NewLineOnAttributes = True

    Try
    Dim writer As XmlWriter = XmlWriter.Create(System.IO.File.Create("somefile.xml"))
        writer.WriteStartDocument(True)
        writer.WriteStartElement("root")
        For rowCounter As Integer = ds.Tables(0).Rows.Count - 1 To 0 Step -1
            writer.WriteStartElement("someelement")
            writer.WriteElementString("col0", ds.Tables(0).Rows(rowCounter)(0).ToString)
            writer.WriteElementString("col1", ds.Tables(0).Rows(rowCounter)(1).ToString)
            writer.WriteElementString("col2", ds.Tables(0).Rows(rowCounter)(2).ToString)
            writer.WriteElementString("col3", ds.Tables(0).Rows(rowCounter)(3).ToString)
            writer.WriteElementString("col4", ds.Tables(0).Rows(rowCounter)(4).ToString)
            writer.WriteElementString("col5", ds.Tables(0).Rows(rowCounter)(5).ToString)
            writer.WriteElementString("col6", ds.Tables(0).Rows(rowCounter)(6).ToString)
            writer.WriteElementString("col7", ds.Tables(0).Rows(rowCounter)(7).ToString)
            writer.WriteEndElement()
        Next
        writer.WriteEndElement()
        writer.WriteEndDocument()
    Catch ex As System.IO.IOException
        MessageBox.Show(ex.Message)
    Finally
        writer.Flush()
        writer.Close()
    End Try

解决方案

What you are missing, is XmlWriterSettings. You declare it, but you don't use it, and when you don't set CloseOutput by hand, the default is false, which means that the output is not closed (in this case your filestream).

To have it behave the way you want it, change your code like this:

Dim settings = New XmlWriterSettings()
settings.Indent = True
settings.IndentChars = " "
settings.NewLineOnAttributes = True
settings.CloseOutput = True             ' <<<< the change '

Using writer As XmlWriter = XmlWriter.Create(System.IO.File.Create("somefile.xml"), settings)
    '.... etc'
End Using

In case you're wondering about how this really works internally, here's the Close method of XmlEncodedRawTextWriterIndent, the internal XmlWriter used in your scenario.

// courtesy of Red Gate's Reflector
public override void Close()
{
    this.FlushBuffer();
    this.FlushEncoder();
    this.writeToNull = true;
    if (this.stream != null)
    {
        this.stream.Flush();
        if (this.closeOutput)      //this flag is set to settings.CloseOutput
        {
            this.stream.Close();
        }
        this.stream = null;
    }
    else if (this.writer != null)
    {
        this.writer.Flush();
        if (this.closeOutput)
        {
            this.writer.Close();
        }
        this.writer = null;
    }
}

这篇关于为什么XMLWriter的不关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆