2008-10-12 163 views
40

我正在C#中創建一個輕量級編輯器,並想知道將字符串轉換爲格式良好的XML字符串的最佳方法。我希望在C#庫中有一個公共方法,例如「public bool FormatAsXml(string text,out string formattedXmlText)」,但它不是那麼容易,可以嗎?在C#中,將字符串格式化爲XML的最佳方法是什麼?

非常特別的是,什麼方法「SomeMethod」必須是會產生下面的輸出?

string unformattedXml; 
string formattedXml; 

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>" 
formattedXml = SomeMethod(unformattedXml); 

Console.WriteLine(formattedXml); 

輸出:

<?xml version="1.0"?> 
    <book id="123"> 
    <author>Lewis, C.S.</author> 
    <title>The Four Loves</title> 
    </book> 

回答

69
string unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"; 
string formattedXml = XElement.Parse(unformattedXml).ToString(); 
Console.WriteLine(formattedXml); 

輸出:

<book> 
    <author>Lewis, C.S.</author> 
    <title>The Four Loves</title> 
</book> 

XML聲明是不是toString()方法的輸出,但它是由Save()...

XElement.Parse(unformattedXml).Save(@"C:\doc.xml"); 
    Console.WriteLine(File.ReadAllText(@"C:\doc.xml")); 

輸出:

<?xml version="1.0" encoding="utf-8"?> 
<book> 
    <author>Lewis, C.S.</author> 
    <title>The Four Loves</title> 
</book> 
+0

謝謝,這正是我之後的:) – thatuxguy 2012-07-03 10:32:32

+0

看起來好像Parse()方法不解析沒有XML聲明的字符串。 – 2016-07-15 05:33:26

0

是字符串有效的XML?你是說如何將XML字符串轉換爲XML文檔?如果是這樣,這樣做:

XmlDocument xml = new XmlDocument(); 

xml.LoadXml(YourString); 
+1

你有沒有看到兩個或三個其他答案說完全相同的東西? – cjk 2010-08-13 08:20:33

5

這聽起來像你想的XML加載到一個XmlTextWriter對象並設置格式和縮進屬性:

writer.Formatting = Formatting.Indented; 
writer.Indentation = 1; 
writer.IndentChar = '\t'; 
+0

我以前使用過這種方法(相對容易),但對於.NET 2.0及更高版本,Microsoft現在推薦使用XmlTextWrtierSettings類,以便您可以利用2.0和3.5中添加的新功能。請參閱我答案中的鏈接。 – Ash 2008-10-12 02:28:45

15

不幸的是沒有,這不是一個容易FormatXMLForOutput方法,這是微軟在這裏討論的;)

無論如何,從.NET 2.0開始,推薦的方法是使用XMlWriterSettingsClass來設置格式,而不是直接在XmlTextWriter對象上設置屬性。 See this MSDN page瞭解更多詳情。它說:

「在.NET Framework 2.0版本中,推薦的做法是使用XmlWriter.Create方法和XmlWriterSettings類創建XmlWriter實例,這樣可以充分利用所引入的所有新功能。本新聞稿中有關更多信息,請參見創建XML編寫「

這裏是推薦的方法的一個例子:

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.Indent = true; 
settings.IndentChars = (" "); 
using (XmlWriter writer = XmlWriter.Create("books.xml", settings)) 
{ 
    // Write XML data. 
    writer.WriteStartElement("book"); 
    writer.WriteElementString("price", "19.95"); 
    writer.WriteEndElement(); 
    writer.Flush(); 
} 
13

使用新的System.Xml.Linq的命名空間(System.Xml.Linq的組件)你可以使用以下內容:

string theString = "<nodeName>blah</nodeName>"; 
XDocument doc = XDocument.Parse(theString); 

你也可以創建一個片段:

string theString = "<nodeName>blah</nodeName>"; 
XElement element = XElement.Parse(theString); 

如果字符串尚未XML,你可以做這樣的事情:

string theString = "blah"; 
//creates <nodeName>blah</nodeName> 
XElement element = new XElement(XName.Get("nodeName"), theString); 

的東西在這個最後的例子要注意的是XElement將XML編碼提供的字符串。

我強烈推薦新的XLINQ類。它們重量更輕,並且更容易使用大多數現有的與XmlDocument相關的類型。

1

如果你只需要轉義XML字符以下可能是有用的:

string myText = "This & that > <> &lt;"; 
myText = System.Security.SecurityElement.Escape(myText); 
4

賈森的方法是最簡單的。這裏的方法:

private static string FormatXmlString(string xmlString) 
{ 
    System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse(xmlString); 
    return element.ToString(); 
} 
9

假設your're只是想,如果你使用的是.NET 3.5或以上的話,最好的解決方案重新格式化XML文檔換上新線新節點並添加縮進,然後,是解析然後用XDocument輸出,類似於:

string unformattedXml; 
string formattedXml; 

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"; 
formattedXml = System.Xml.Linq.XDocument.Parse(unformattedXml).ToString(); 

Console.WriteLine(formattedXml); 

整潔的胡?

這應該重新格式化XML節點。

要做到這一點與以前版本的框架需要更多的工作,因爲沒有內置函數來重新計算空白。

事實上,使用前LINQ的類會做到這一點:

string unformattedXml; 
string formattedXml; 

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"; 
System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
doc.LoadXml(unformattedXml); 
System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
System.Xml.XmlWriter xw = System.Xml.XmlTextWriter.Create(sb, new System.Xml.XmlWriterSettings() { Indent = true }); 
doc.WriteTo(xw); 
xw.Flush(); 
formattedXml = sb.ToString(); 
Console.WriteLine(formattedXml); 
0

System.Xml.Linq.XElement.ToString()自動格式化!

XElement formattedXML = new XElement.Parse(unformattedXmlString); 
Console.WriteLine(formattedXML.ToString()); 
1

在Framework 4.0它簡單。

var unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"; 
var xdoc = System.Xml.Linq.XDocument.Parse(unformattedXml); 
var formattedXml = (xdoc.Declaration != null ? xdoc.Declaration + "\r\n" : "") + xdoc.ToString(); 
Console.WriteLine(formattedXml); 

這增加了在所需的縮進和保持XML聲明

<?xml version="1.0"?> 
<book> 
    <author>Lewis, C.S.</author> 
    <title>The Four Loves</title> 
</book>