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 6326629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:07:39+00:00 2026-05-24T17:07:39+00:00

I have created an XDocument object containing some exception data, I want to save

  • 0

I have created an XDocument object containing some exception data, I want to save this to an XML file so that it can be reviewed when investigating errors:

XDocument xmlDoc = XDocument.Parse(myCustomObject.SerializeToXml());

I’ve using the Save() method to save this data to file:

xmlDoc.Save(@"C:\myFile.xml", SaveOptions.None);

One of the nodes is output containing escaped characters (< and >), for example

<RootNode>
  <Name>My Object</Name>
  <DiagnosticInfo>
    &lt;DiagnosticInfo&gt;
      &lt;Processors&gt;
        &lt;String&gt;
          Something Something
        &lt;String&gt;
      &lt;/Processors&gt;
    &lt;/DiagnosticInfo&gt;
  </DiagnosticInfo>
</RootNode>

I’ve tried calling HttpUtility.DecodeHtml() on the “DiagnosticInfo” XElement but it is always output as escaped, any way round this? Do I have to do something to the DiagnosticInfo element to tell it not to treat its content as a string?

The DiagnosticInfo element is the one that would be most useful so I’d like to have it looking proper when the XML file is opened.

Edit
In response to Jon Skeet’s comment:

public string SerializeToXml()
{
    XmlSerializer s = new XmlSerializer(GetType());

    using (StringWriter w = new StringWriter())
    {
        s.Serialize(w, this);
        return w.ToString();
    }
}
  • 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-24T17:07:40+00:00Added an answer on May 24, 2026 at 5:07 pm

    .Net serialisation uses reflection to decide what XML Elements to generate. These are based on the property names (like Name, DiagnosticInfo).

    You mention DiagnosticInfo is an XElement. That means it is not a hierarchy of other objects, which is what you wanted, but rather it is a parser of XML elements and actually stores its XML as a string. When you serialise it you just get a single content value as a string.

    You either need to parse/de-serialize your “info” into a more complex structure that actually contains properties like "Processors", or write out the XML manually using an XmlTextWriter and .WriteStartElement(), WriteEndElement() etc

    Writing it out manually, rather than trying to serialize it, is quite easy and would be my choice. The only unusual method you will need is XmlTextWriter.WriteRaw which will stream out your raw XML in the middle of your other writes.

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

Sidebar

Related Questions

I have an xml file that looks like this; <Employee> <EmployeeName>Burt Reynolds</EmployeeName> <EmployeeTitle>Bad Ass</EmployeeTitle>
I have an existing XDocument object that I would like to add an XML
I have this XML file that I parse into its elements and create a
i have created an xml file from my c# application i want to use
I have new XmlDocument object, i.g. xml is created during my program... I want
I want to send an xml document that i have created using jdom api.
I have created a file with XmlDocument xmldoc = new XmlDocument(); Can I make
this is my situation: I have two org.w3c.dom.Document created from two xml files. What
I have an XML document as follows: <directory> <file><monitored>0</monitored> <xferStatus>1</xferStatus> <name>test1.txt</name> <size>7</size> <created>03/31/10 11:30:02
I have an object which I have created from an XML document using visual

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.