Solved

linq to XML doc.save is failing, c#

Posted on 2014-01-24
6
287 Views
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";
doc.Save(fName);


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?
0
Comment
Question by:g_johnson
  • 2
  • 2
  • 2
6 Comments
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39808032
does this help?

http://stackoverflow.com/questions/4533609/how-to-get-xml-as-string-from-xdocument

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

Author Comment

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

Expert Comment

by:Fernando Soto
ID: 39825828
Can you post the code that builds the XML document please?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 12

Assisted Solution

by:deanvanrooyen
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...
0
 
LVL 4

Author Comment

by:g_johnson
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.
0
 
LVL 63

Accepted Solution

by:
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.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

822 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