简体   繁体   中英

xmlwriter , it's being used by another process

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

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

 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.

 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. !

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 .

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

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).

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM