如何将普通XML分配给C#变量

5

可能是重复问题:
如何在C#中使用XML文字?

我可以在Visual Basic中做到这一点,那我该如何在C#中实现呢?

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

C#(至少4及以下版本)不支持XML文字。在此处,new XElement("Employees")可以工作,但XElement还可以解析更大的[verbatim]文本。 - user166390
https://dev59.com/nWoy5IYBdhLWcg3wHaXt , http://www.codeproject.com/Tips/84852/TRICK-Xml-Literals-for-C - Mitch Wheat
2个回答

10

尝试:

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


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接