2016-10-04 37 views
1

我有一個XML文件,結構就像是以下幾點:LINQ的讀取XML與<br>標籤

<template><body>public DiffSectionType Type<template:br/>{<template:br/><template:tab/>get<template:br/><template:tab/>{<template:br/><template:tab/><template:tab/>return _Type;<template:br/><template:tab/>}<template:br/>}</body></template> 

我想成爲更具可讀性,如:

public DiffSectionType Type 
{ 
    get 
    { 
     return _Type; 
    } 
} 

<template:br/> => new line 
<template:tab/> => tab 

我能讀懂身體的字符串,但不能把它放在正確的格式, 我已經試過

var document = XDocument.Load("template.xml"); 
var body = from element in document.Elements("template").Elements("body") 
       select element; 
foreach(var v in body) 
{ 
    Console.WriteLine(v.Value); 
} 
+0

你能用''嗎?但即便如此,我也不會使用行來解析xml。 XML的目的是使用元素來定義結構,而不是行。 – smoksnes

+0

這是html而不是xml。 – jdweng

回答

0

你可以使用雷傑x解決這個這樣的事情是這樣的:

 string str = @"<template><body>public DiffSectionType Type<template:br/>{<template:br/><template:tab/>get<template:br/><template:tab/>{<template:br/><template:tab/><template:tab/>return _Type;<template:br/><template:tab/>}<template:br/>}</body></template>"; 
     str = Regex.Replace(str, "<template:br\x2F>", Environment.NewLine); 
     str = Regex.Replace(str, "<template:tab\x2F>", "\t"); 
     str = Regex.Replace(str, "(<\x2Ftemplate>)|(<template>)", ""); 
     str = Regex.Replace(str, "(<\x2Fbody>)|(<body>)", "");