将简单文本转换为XML

isp*_*iro 0 .net c# xml

我想将字符串转换为XML.当然我可以这样做:

"<node Attribute1="att1">" + MyString + "</node>"
Run Code Online (Sandbox Code Playgroud)

但是,如果在.net中存在某些东西,为什么要重新发明轮子呢?是否有一个方法采用节点名称,属性和内部XML并返回XML字符串?

L.B*_*L.B 6

你可以使用Linq To Xml

var xElem = new XElement("node", new XAttribute("Attribute1", "att1"), "MyString");
var xml = xElem.ToString();
Run Code Online (Sandbox Code Playgroud)

会给你的

<node Attribute1="att1">MyString</node>
Run Code Online (Sandbox Code Playgroud)