Stig Perez
Stig Perez

Reputation: 3813

How do I remove indention with XDocument.Load

I have a simple XML-file with only one line in it:

<Foo><Bar>foobar</Bar></Foo>

I use XDocument to load the content in a console application, like so:

XDocument xDoc = XDocument.Load(pathToXmlFile)

If I use Console.WriteLine to output the xDocvariable I get an indented version of the line:

<Foo>
    <Bar>foobar</Bar>
</Foo>

It also looks indented when inspecting the variable at runtime. My question is how I can get rid of the indention when loading the XML so that it still only occupies one line?

Upvotes: 0

Views: 217

Answers (1)

dbasnett
dbasnett

Reputation: 11773

Try this

    Dim xe As XElement = <root><el></el></root>
    Dim s As String = xe.ToString(SaveOptions.DisableFormatting)
    Debug.WriteLine(s)

outputs

<root><el></el></root>

Without SaveOptions.DisableFormatting the output is

<root>
  <el></el>
</root>

Upvotes: 2

Related Questions