xmlwriter,它被另一个进程使用

时间:2022-06-24 21:04:20

Here, i created a class, and what i am trying to accomplish is to write the contents from the list into an xml file.

在这里,我创建了一个类,我想要完成的是将列表中的内容写入xml文件。

1) At first run, it creates the file and trows an error here: Token EndElement in state EndRootElement would result in an invalid XML document

1)首次运行时,它会创建文件并在此处产生错误:状态EndRootElement中的Token EndElement将导致无效的XML文档

 public static void SaveCellPhoneProducts(List<ProducCellPhone> LocalProducts)
    {
        XmlWriterSettings localSettings = new XmlWriterSettings();
        localSettings.Indent = true;
        localSettings.IndentChars = ("   ");

        //second run, error Occurr here
        //xml writer object, CellPhoneProduct
        XmlWriter xmlOut = XmlWriter.Create(path, localSettings);

        xmlOut.WriteStartDocument();
        xmlOut.WriteStartElement("Cell");

        foreach(ProducCellPhone localProduct in LocalProducts)
        {
            WriteCellPhoneProductBase(localProduct, xmlOut);
        }
        //first Run error is thrown in here.  
        xmlOut.WriteEndElement();
        xmlOut.Close();
    }

2) When i rerun on the second time, the error is in same method.

2)当我第二次重新运行时,错误是相同的方法。

 public static void SaveCellPhoneProducts(List<ProducCellPhone> LocalProducts)
    {
        XmlWriterSettings localSettings = new XmlWriterSettings();
        localSettings.Indent = true;
        localSettings.IndentChars = ("   ");

        //xml writer object, CellPhoneProduct
        XmlWriter xmlOut = XmlWriter.Create(path, localSettings);

        xmlOut.WriteStartDocument();
        xmlOut.WriteStartElement("Cell");

        foreach(ProducCellPhone localProduct in LocalProducts)
        {
            WriteCellPhoneProductBase(localProduct, xmlOut);
        }
        xmlOut.WriteEndElement();
        xmlOut.Close();
    }

The whole class i here:

全班我在这里:

class ProductCellPhoneDB
{
    private const string path = @"..\..\CellPhoneProducts.xml";

    public static List<ProducCellPhone> GetCellPhoneProducts()
    {
        List<ProducCellPhone> localCellPhoneProducts = 
            new List<ProducCellPhone>();

        XmlReaderSettings localSettings = new XmlReaderSettings();
        localSettings.IgnoreWhitespace = true;
        localSettings.IgnoreComments = true;

        XmlReader xmlIn = (XmlReader.Create(path,localSettings));
        if (xmlIn.ReadToDescendant("Cell"))
        {
            do
            {
                ProducCellPhone localProduct = null;
                xmlIn.ReadStartElement("Cell");
                localCellPhoneProducts.Add(localProduct);
            }
            while (xmlIn.ReadToNextSibling("Cell"));
        }
        xmlIn.Close();
        return localCellPhoneProducts;       
    }

    public static void SaveCellPhoneProducts(List<ProducCellPhone> LocalProducts)
    {
        XmlWriterSettings localSettings = new XmlWriterSettings();
        localSettings.Indent = true;
        localSettings.IndentChars = ("   ");

        //Error Occurr here
        //xml writer object, CellPhoneProduct, error is being used by other process?
        XmlWriter xmlOut = (XmlWriter.Create(path, localSettings));

        xmlOut.WriteStartDocument();
        xmlOut.WriteStartElement("Cell");

        foreach(ProducCellPhone localProduct in LocalProducts)
        {
            WriteCellPhoneProductBase(localProduct, xmlOut);
        }
        //ERROR  Token EndElement in state EndRootElement would result in an invalid XML document
        xmlOut.WriteEndElement();
        xmlOut.Close();
    }

    private static void ReadCellphoneProductBase(XmlReader xmlIn, ProducCellPhone localProduct)
    {
        localProduct.Iemi = xmlIn.ReadElementContentAsString();
        localProduct.Model = xmlIn.ReadContentAsString();
        localProduct.Price = xmlIn.ReadContentAsDecimal();            
    }

    private static void WriteCellPhoneProductBase(ProducCellPhone localProduct,
        XmlWriter xmlout)
    {
        xmlout.WriteElementString("IEMI", localProduct.Iemi);
        xmlout.WriteElementString("Model", localProduct.Model);
        xmlout.WriteElementString("Price", Convert.ToString(localProduct.Price));
        xmlout.WriteEndElement();
    }
}

Any suggestions would be helpful. Thanks community. !

任何的意见都将会有帮助。谢谢社区。 !

1 个解决方案

#1


The first error you get is likely because the WriteStartElement and WriteEndElement calls are not matched. You do xmlOut.WriteStartElement("Cell"); once, but do xmlout.WriteEndElement(); several times, once for each ProducCellPhone in LocalProducts, plus another time after the foreach.

您获得的第一个错误可能是因为WriteStartElement和WriteEndElement调用不匹配。你做xmlOut.WriteStartElement(“Cell”);一次,但是做xmlout.WriteEndElement();几次,一次为LocalProducts中的每个ProducCellPhone,再加上foreach之后的另一次。

To solve this (if I guessed your XML document structure right), you should change your WriteCellPhoneProductBase method to:

要解决这个问题(如果我猜对了你的XML文档结构),你应该将WriteCellPhoneProductBase方法更改为:

private static void WriteCellPhoneProductBase(ProducCellPhone localProduct,
    XmlWriter xmlout)
{
    xmlOut.WriteStartElement("Cell");
    xmlout.WriteElementString("IEMI", localProduct.Iemi);
    xmlout.WriteElementString("Model", localProduct.Model);
    xmlout.WriteElementString("Price", Convert.ToString(localProduct.Price));
    xmlout.WriteEndElement();
}    

And remove the WriteStartElement and WriteEndElement lines from SaveCellPhoneProducts (see below).

并从SaveCellPhoneProducts中删除WriteStartElement和WriteEndElement行(见下文)。

The second error is probably because the XmlWriter used when you got the first error was not disposed and has not closed the file handle. You should always use a using block to ensure IDisposable resources get disposed, also when an exception occurs. You should change your method to:

第二个错误可能是因为没有处理第一个错误时使用的XmlWriter并且没有关闭文件句柄。您应始终使用using块来确保在发生异常时处置IDisposable资源。您应该将方法更改为:

public static void SaveCellPhoneProducts(List<ProducCellPhone> LocalProducts)
{
    //xml writer settings
    XmlWriterSettings localSettings = new XmlWriterSettings();
    localSettings.Indent = true;
    localSettings.IndentChars = ("   ");

    using (var xmlOut = XmlWriter.Create(path, localSettings))
    {
        xmlOut.WriteStartDocument();

        //write each product on xml file
        foreach(ProducCellPhone localProduct in LocalProducts)
            WriteCellPhoneProductBase(localProduct, xmlOut);

        xmlOut.WriteEndDocument()
    }
}

For your GetCellPhoneProducts follow the same using block approach.

对于您的GetCellPhoneProducts,请遵循相同的使用块方法。

#1


The first error you get is likely because the WriteStartElement and WriteEndElement calls are not matched. You do xmlOut.WriteStartElement("Cell"); once, but do xmlout.WriteEndElement(); several times, once for each ProducCellPhone in LocalProducts, plus another time after the foreach.

您获得的第一个错误可能是因为WriteStartElement和WriteEndElement调用不匹配。你做xmlOut.WriteStartElement(“Cell”);一次,但是做xmlout.WriteEndElement();几次,一次为LocalProducts中的每个ProducCellPhone,再加上foreach之后的另一次。

To solve this (if I guessed your XML document structure right), you should change your WriteCellPhoneProductBase method to:

要解决这个问题(如果我猜对了你的XML文档结构),你应该将WriteCellPhoneProductBase方法更改为:

private static void WriteCellPhoneProductBase(ProducCellPhone localProduct,
    XmlWriter xmlout)
{
    xmlOut.WriteStartElement("Cell");
    xmlout.WriteElementString("IEMI", localProduct.Iemi);
    xmlout.WriteElementString("Model", localProduct.Model);
    xmlout.WriteElementString("Price", Convert.ToString(localProduct.Price));
    xmlout.WriteEndElement();
}    

And remove the WriteStartElement and WriteEndElement lines from SaveCellPhoneProducts (see below).

并从SaveCellPhoneProducts中删除WriteStartElement和WriteEndElement行(见下文)。

The second error is probably because the XmlWriter used when you got the first error was not disposed and has not closed the file handle. You should always use a using block to ensure IDisposable resources get disposed, also when an exception occurs. You should change your method to:

第二个错误可能是因为没有处理第一个错误时使用的XmlWriter并且没有关闭文件句柄。您应始终使用using块来确保在发生异常时处置IDisposable资源。您应该将方法更改为:

public static void SaveCellPhoneProducts(List<ProducCellPhone> LocalProducts)
{
    //xml writer settings
    XmlWriterSettings localSettings = new XmlWriterSettings();
    localSettings.Indent = true;
    localSettings.IndentChars = ("   ");

    using (var xmlOut = XmlWriter.Create(path, localSettings))
    {
        xmlOut.WriteStartDocument();

        //write each product on xml file
        foreach(ProducCellPhone localProduct in LocalProducts)
            WriteCellPhoneProductBase(localProduct, xmlOut);

        xmlOut.WriteEndDocument()
    }
}

For your GetCellPhoneProducts follow the same using block approach.

对于您的GetCellPhoneProducts,请遵循相同的使用块方法。