Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to load xml into an XDocument without indentation

9 views
Skip to first unread message

Rahul Aggarwal

unread,
Nov 6, 2009, 4:55:00 PM11/6/09
to
Hi,
 
I am trying to load xml into XDocument using XDocument.Load(filename). The cml in the file is not indented but when the xml gets loaded into the XDocument, it gets indented. The content I am loading is mixed content which causes issues for me. I have looked a lot but have not been able to figure out the answer.
 
XDocument.Load(filename)  is indenting the XML that is being loaded
 
Thanks
Rahul

Martin Honnen

unread,
Nov 7, 2009, 6:10:32 AM11/7/09
to
Rahul Aggarwal wrote:

> I am trying to load xml into XDocument using XDocument.Load(filename).
> The cml in the file is not indented but when the xml gets loaded into
> the XDocument, it gets indented. The content I am loading is mixed
> content which causes issues for me. I have looked a lot but have not
> been able to figure out the answer.
>

> *XDocument.Load(filename) is indenting the XML that is being loaded*

Both the Load method and the Save method have options to control
indendation.

You probably want to use
XDocument doc = XDocument.Load(filename, LoadOptions.PreserveWhitespace);
and then
doc.Save(filename, SaveOptions.DisableFormatting);

See http://msdn.microsoft.com/en-us/library/bb387014.aspx and
http://msdn.microsoft.com/en-us/library/bb387103.aspx

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

0 new messages