Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7437409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:23:45+00:00 2026-05-29T10:23:45+00:00

I am trying to serialize my Report class info to an XML. At this

  • 0

I am trying to serialize my Report class info to an XML. At this point I think all of the serialize and deserialize code works, but for the initial write, I’m having trouble performing the serialize, because the XML file doesn’t exist yet.

for an empty text file, i can use:

StreamWriter sw = File.CreateText(@"path");
sw.Close();

this is my code block for the serializing. the exception (Directory not found) is getting thrown on the StreamWriter line. I’d like to simply add an if(!File.Exists(xmlPath))...create empty XML. Or maybe there is a more correct way to do this.

public void SerializeToXML(Report newReport)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Report));
            TextWriter textWriter = new StreamWriter(xmlPath);
            serializer.Serialize(textWriter, newReport);
            textWriter.Close();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:23:46+00:00Added an answer on May 29, 2026 at 10:23 am

    The StreamWriter(String) constructor will create the file if it does not already exist:

    If the file exists, it is overwritten; otherwise, a new file is created.

    However, it will not create any inexistent directories in your path.

    DirectoryNotFoundException: The specified path is invalid, such as being on an unmapped drive.

    To create any required directories, you can include the following code (at the beginning of your SerializeToXML method):

    var dir = Path.GetDirectoryName(xmlPath);
    if (!Directory.Exists(dir))
        Directory.CreateDirectory(dir);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm trying serialize a class containing this property: [NonSerialized] property System::Collections::ObjectModel::ReadOnlyCollection<String^>^ IgnoredWords I
I'm trying to serialize/deserialize string. Using the code: private byte[] StrToBytes(string str) { BinaryFormatter
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
I am trying to serialize my object to xml. A serializer seemingly serializes all
I'm trying to serialize an object to XML that has a number of properties,
I have been trying to serialize a pretty big object. This object uses dictionaries
i'm trying to serialize DataTable to Json or XML. is it possibly and how?
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
I've been trying to serialize an XML (jQuery object) to string to POST it
I'm trying to serialize into a Xml File an object which is declared as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.