2012-09-13 112 views

回答

0

使用此:

public static string StripXmlWhitespace(string Xml) 
{ 
    Regex Parser = new Regex(@">\s*<"); 
    Xml = Parser.Replace(Xml, "><"); 

    return Xml.Trim(); 
} 
0

您可以使用字符串的替換方法格式化的xmlString,然後將其保存到輸出:

string singleLineXml = xml.Replace(System.Environment.NewLine, " ") 

string singleLineXml = xml.Replace("\r\n", " ") 

除去換行後>刪除空格:

singleLineXml.Remove(' '); 

是@Steve Wellens,刪除('')是個不錯的主意。讓我們試試

singleLineXml.Replace("> <","><"); 

,我發現相對線程,可能是它有助於Writing string to XML file without formatting (C#)

+0

這將從節點內移除空間。 –

+0

我使用XmlDocument來讀取整個xml,因此我沒有Remove或Replace屬性。 – user1658567