Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,158 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,938 people online right now. Registration is fast and FREE... Join Now!




Help creating and writing to XML document

 
Reply to this topicStart new topic

Help creating and writing to XML document

PsychoCoder
22 Dec, 2007 - 05:17 AM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Ok, this is what I gt for writing code so early and without full cup of coffee in me (not to mention to medication lol), but in one method I create a file => urlList.xml => like so:

CODE

private string BuildUrlFile()
{
    //first build our variables and paths
        string filePath = GetFolderPath();
        //variable to hold he file and path
        string returnValue;
    try
    {
        //now check to see if the file exists
        if (!(File.Exists(filePath + "\\" + _file)))
        {
            //file doesnt exists so we need to create it
            File.Create(filePath + "\\" + _file);
        }
      
        returnValue = filePath + "\\" + _file;
    }
    catch (DirectoryNotFoundException ex)
    {
        MessageBox.Show(ex.Message);
        returnValue= string.Empty;
    }
    catch (FileNotFoundException ee)
    {
        MessageBox.Show(ee.Message);
        returnValue= string.Empty;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        returnValue = string.Empty;
    }
    return returnValue;
}


Which of course works fine, creates the document right where I want it. But when I try to write to this newly created document, the first time, when it is created, I get an exception telling me I cannot write to the file because it is being accesses by another resource.

The code I'm using to write to the XML file is as follows:

CODE

public void createUrlList()
{
    try
    {
        //build out XMLTextWriter Object so we can add this url to the list
        XmlTextWriter writer = new XmlTextWriter(BuildUrlFile(),null);
        //set the XML formatting
        writer.Formatting = Formatting.Indented;
        //write the opening element of the XML doc
        writer.WriteStartDocument(false);
        //write document type
        writer.WriteDocType("url",null,"url.dtd",null);
        //write a comment, so all will know what this file is for
        writer.WriteComment("This file will hold all the url's the user types into their address bar");
        //now we write the start element
        writer.WriteStartElement("urlList");
            //now we start the document URL element
            writer.WriteStartElement("URL",null);
            //now we start writing our attributes of the URL
            //element: Name, URL, Date
            writer.WriteAttributeString("SiteName",_siteName);
            writer.WriteAttributeString("SiteURL",_url);
            writer.WriteAttributeString("DateAdded",_added.ToString());
        //now we end the elements of the document
        writer.WriteEndAttribute();
        //close our 2 start elements
        writer.WriteEndElement();
        writer.WriteEndElement();
        //now end the document
        writer.WriteEndDocument();

        //now we flush and close the writer to clear the buffer
        //and any memory & resources being held by the writer
        writer.Flush();
        writer.Close();
    }
    catch(XmlException ex)
    {
      
        MessageBox.Show(ex.Message);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}


Now no one has ever considered me the "XML Guru" at work now lol, so no laughing at the code. I know if I create a new document with a StreamWriter or the likes I have to close said object or it holds it open so nothing else can manipulate it, but I don't even remember running into this using File.Create().

Now the 2nd time the application runs of course there are no problems accessing the file (there are however errors when trying to write to the XML document, guess I got my closing attributes or something outta whack or order or something, but it would be nice if someone could help me with the first issue smile.gif
User is online!Profile CardPM
+Quote Post

Jayman
RE: Help Creating And Writing To XML Document
22 Dec, 2007 - 12:08 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
I believe that the issue is when using the static methods of File they are not implicitly closed. Although I cannot find anything in the documentation that says as much.

However, you can use the File.Create along with a FileStream object. Then you can explicitly close the connection, which should alleviate the problem.

CODE

FileStream fs = File.Create(filePath + "\\" + _file);
fs.Close();

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:42PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month