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

  • Home
  • SEARCH
  • 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 6669501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:09:26+00:00 2026-05-26T03:09:26+00:00

I would like to XML serialize instances of my object Exception and store it

  • 0

I would like to XML serialize instances of my object Exception and store it in the XMLNode[] Nodes property of another object ExceptionReport.

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
public partial class ExceptionReport : object, System.Xml.Serialization.IXmlSerializable
{
    public System.Xml.XmlNode[] Nodes { get; set; }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        this.Nodes = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
    }
}
public class Exception
{
    public string ExceptionText;
    public string exceptionCode;
    public string locator;
}

How would i go about doing this so the result would be something like this:

<ExceptionReport xmlns="http://www.opengis.net/ows" >
  <Exception exceptionCode="1">my first instance</Exception>
  <Exception exceptionCode="2">my second instance</Exception>
</ExceptionReport>

So far i have the following but i need to know how to serialize these objects and store them in the ExceptionReport Nodes array.

ExceptionReport er = new ExceptionReport();

Exception exception_item1 = new Exception();
exception_item1.ExceptionText = "my first instance";
exception_item1.exceptionCode = "1";

Exception exception_item2 = new Exception();
exception_item2.ExceptionText = "my second instance";
exception_item2.exceptionCode = "2";

List<Exception> exceptions = new List<Exception>( exception_item1, exception_item2 );
  • 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-26T03:09:27+00:00Added an answer on May 26, 2026 at 3:09 am
    [XmlRoot("ExceptionReport")]
        public partial class ExceptionReport
        {
            [XmlElement("Exception")]
            public List<Exception> Nodes { get; set; }
    
            public ExceptionReport()
            {
                Nodes = new List<Exception>();
            }
        }
        public class Exception
        {
            [XmlText]
            public string ExceptionText;
            [XmlAttribute("exceptionCode")]
            public int ExceptionCode;
            [XmlAttribute("locator")]
            public string Locator;
        }
    

    Then to serialize, I use the following extensions:

    public static bool XmlSerialize<T>(this T item, string fileName)
            {
                return item.XmlSerialize(fileName, true);
            }
            public static bool XmlSerialize<T>(this T item, string fileName, bool removeNamespaces)
            {
                object locker = new object();
    
                XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
                xmlns.Add(string.Empty, string.Empty);
    
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
    
                lock (locker)
                {
                    using (XmlWriter writer = XmlWriter.Create(fileName, settings))
                    {
                        if (removeNamespaces)
                        {
                            xmlSerializer.Serialize(writer, item, xmlns);
                        }
                        else { xmlSerializer.Serialize(writer, item); }
    
                        writer.Close();
                    }
                }
    
                return true;
            }
            public static T XmlDeserialize<T>(this string s)
            {
                object locker = new object();
                StringReader stringReader = new StringReader(s);
                XmlTextReader reader = new XmlTextReader(stringReader);
                try
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    lock (locker)
                    {
                        T item = (T)xmlSerializer.Deserialize(reader);
                        reader.Close();
                        return item;
                    }
                }
                finally
                {
                    if (reader != null)
                    { reader.Close(); }
                }
            }
            public static T XmlDeserialize<T>(this FileInfo fileInfo)
            {
                string xml = string.Empty;
                using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        return sr.ReadToEnd().XmlDeserialize<T>();
                    }
                }
            }
    

    Use like this:

    ExceptionReport report = new ExceptionReport();
                report.Nodes.Add(new Exception { ExceptionText = "my first instance", ExceptionCode = 1, Locator = "loc1" });
                report.Nodes.Add(new Exception { ExceptionText = "my second instance", ExceptionCode = 2 });
                report.XmlSerialize("C:\\test.xml");
    

    I tested and it came out like you wanted. Hope it helps…

    PS – The extensions came from my library on codeproject: http://www.codeproject.com/KB/dotnet/MBGExtensionsLibrary.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to XML serialize an object that has (among other) a property
I'm serializing an object to xml in c# and I would like to serialize
I am using a C# object to Serialize/Deserialize XML. I would like to add
I would like to serialize an object to XML inside Android. Any libs suggested?
I would like to reflect the XML tree in my object structure, but I
When serializing, I would like to serialize an object only once, then any references
I have a model that I would like to serialize to an xml with
I have a simple object graph that I would like to serialize, I haven't
I am using the XML serializer. I would like it to convert all my
I would like to create XML Schema for this chunk of xml, I would

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.