简体   繁体   English

XML文件到datagridview C#

[英]XML file to datagridview c#

I am using an api that returns text in XML that I am saving to an XML file. 我正在使用一个API,该API返回以XML格式保存的XML文件中的文本。 Whenever I am trying to display the information to a datagridview. 每当我尝试将信息显示到datagridview时。 But I get an error saying the file is already open and being used. 但是我收到一个错误消息,说该文件已经打开并正在使用。 Here is the code that receives the text, saves it to the XML and tries to display it to the datagrid. 这是接收文本,将其保存到XML并尝试将其显示到数据网格的代码。

     using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader sr99 = new StreamReader(stream))
                {
                    responseContent = sr99.ReadToEnd();
                }
            }
        }
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(responseContent);
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        // Save the document to a file and auto-indent the output.
        XmlWriter writer = XmlWriter.Create("ResponseContent.xml", settings);
        doc.Save(writer);



        XmlReader xmlFile = XmlReader.Create(@"C:\Users\Tyler\Documents\Repo\New Trunk\WalmartSmiles\WalmartSmiles\bin\Debug\ResponseContent.xml", new XmlReaderSettings());
        DataSet dataSet = new DataSet();
        //Read xml to dataset
        dataSet.ReadXml("ResponseContent.xml");
        //Pass empdetails table to datagridview datasource
        dataGridView1.DataSource = dataSet.Tables["ns2:feed"];
        //Close xml reader
        xmlFile.Close();
XmlReader xmlFile ;
xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
dataGridView1.DataSource = ds.Tables[0];

Simple way of pass data to dataGridView 将数据传递到dataGridView的简单方法

Usually when this happens, look for other things in your code that have also opened, read, written, or done anything with that file, and close them! 通常,发生这种情况时,请在代码中查找其他也已打开,读取,写入或对该文件执行任何操作的其他事物,然后将其关闭!

    // Save the document to a file and auto-indent the output.
    XmlWriter writer = XmlWriter.Create("ResponseContent.xml", settings);
    doc.Save(writer);
    writer.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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