如何将纯XML分配给C#变量

use*_*086 5 c# xml linq vb6

可能重复:
在C#中使用XML文字?

我可以在Visual Basic中执行此操作,如何在C#中执行此操作

    Dim xmlTree As XElement = <Employees></Employees>
Run Code Online (Sandbox Code Playgroud)

Ale*_*lte 10

尝试:

XDocument document = XDocument.Parse("<Employees></Employees>")
Run Code Online (Sandbox Code Playgroud)

要么

XElement root = new XElement("Employees")
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用XmlDocument类:

XmlDocument document = new XmlDocument();
document.LoadXml("<Employees></Employees>");
Run Code Online (Sandbox Code Playgroud)

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


小智 1

XDocument document = XDocument.Parse("<Employees></Employees>")
Run Code Online (Sandbox Code Playgroud)