xmlwriter,它被另一个进程使用

时间:2015-05-10 03:41:57

标签: c#

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

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)当我第二次重新运行时,错误是相同的方法。

 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();
    }

全班同学在这里:

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();
    }
}

任何建议都会有所帮助。谢谢社区。 !

1 个答案:

答案 0 :(得分:1)

第一个错误  得到的可能是因为WriteStartElementWriteEndElement来电不匹配。您执行xmlOut.WriteStartElement("Cell");一次,但xmlout.WriteEndElement();多次,ProducCellPhone中的LocalProducts一次,foreach之后的另一次。{/ p>

要解决此问题(如果我猜对了您的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();
}    

WriteStartElement移除WriteEndElementSaveCellPhoneProducts行(见下文)。

第二个错误可能是因为当您收到第一个错误时使用的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()
    }
}

对于GetCellPhoneProducts,请遵循相同的using阻止方法。

相关问题