linq to XML is failing, c#

Posted on 2014-01-24
Last Modified: 2016-02-10
I have a c# program that creates an xml document using linq to xml:

XDocment doc = new XDocument();

there are over 600 lines of logic building this xml file, culminating in:

string fName = this._writepath + "file856" + curDateTime + ".xml";

at doc.Save I am getting an error:  This operation would create an incorrectly strucutred document.  

Unfortunately, this error doesn't happen in my development environment, only in the delivery environment (the executable on the client's machine).

Is there anything I can do to get the executable to show me the structure of the xml so that I can see where it's malformed?  In other words, since doc.Save fails, is there some way to throw it into a viewer or a NotePad or something else that would allow me to look at it?
Question by:g_johnson
  • 2
  • 2
  • 2
LVL 12

Expert Comment

ID: 39808032
does this help?

what encoding are you using, be very wary of UT8 or other non standard formats

Author Comment

ID: 39824314
I'm using utf-8.  How does that play into this?  The .ToString() method returned the same error.
LVL 62

Expert Comment

by:Fernando Soto
ID: 39825828
Can you post the code that builds the XML document please?
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

LVL 12

Assisted Solution

deanvanrooyen earned 100 total points
ID: 39827091
typo - should have been utf-8, utf-8 generally should be ok then - you probably constructing a document that is not well formed...

Author Comment

ID: 39840128
Correct -- I agree that it's not well-formed.  I am looking for a way to display it so that I can spot the error.  So far, I have not been able to get any application of .ToString() to do that for me.
LVL 62

Accepted Solution

Fernando Soto earned 400 total points
ID: 39840158
The problem is that when you create the document it goes through a XML validation process and if it fails it will not get loaded and therefore there will nothing to print. To find the issue you will need to go through the code that creates it and find where it is being malformed.

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now