Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flat xml to one line in c# code?

Tags:

c#

.net

xml

How to flat xml to one line in c# code ?

Before:

<CATALOG>
<CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
</CD>
</CATALOG>

After:

<CATALOG><CD><TITLE>Empire Burlesque</TITLE><ARTIST>Bob Dylan</ARTIST>COUNTRY>USA</COUNTRY>....
like image 936
Erez Avatar asked Apr 10 '11 09:04

Erez


2 Answers

Assuming you're able to use LINQ to XML, and the XML is currently in a file:

XDocument document = XDocument.Load("test.xml");
document.Save("test2.xml", SaveOptions.DisableFormatting);
like image 196
Jon Skeet Avatar answered Sep 18 '22 16:09

Jon Skeet


If you have the XML in a string:

xml.Replace("\n", "").Replace("\r", "")
like image 40
Anonymous Avatar answered Sep 18 '22 16:09

Anonymous