2013-01-14 36 views
4

可能重复:
Use XML Literals in C#?如何纯XML分配到C#变量

我可以在Visual Basic中做到这一点,我怎么能做到这一点在C#

Dim xmlTree As XElement = <Employees></Employees> 
+0

C#(至少4个及以下)不*支持XML文字。 '新的XElement(“Employees”)'会在这里工作,但XElement也可以解析更大的[逐字]文本。 – 2013-01-14 04:46:56

+0

http://stackoverflow.com/questions/8768762/use-xml-literals-in-c,http://www.codeproject.com/Tips/84852/TRICK-Xml-Literals-for-C –

回答

1
XDocument document = XDocument.Parse("<Employees></Employees>") 
9

Try:

XDocument document = XDocument.Parse("<Employees></Employees>") 

XElement root = new XElement("Employees") 

另一种方法是使用XmlDocument类:

XmlDocument document = new XmlDocument(); 
document.LoadXml("<Employees></Employees>"); 

,但我建议使用XDocument。它比XmlDocument更新,它有更清洁的API和支持Linq To Xml