C# 将简单文本转换为XML

C# 将简单文本转换为XML,c#,.net,xml,C#,.net,Xml,我想将字符串转换为XML。当然,我可以这样做: "<node Attribute1="att1">" + MyString + "</node>" “”+MyString+“” 但是,如果.net中有相关内容,为什么要重新发明轮子呢。是否有一种方法可以接受节点名、属性和内部XML并返回XML字符串?您可以使用 我会给你 <node Attribute1="att1">MyString</node> MyString 您可以使用 我会给你 &l

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

"<node Attribute1="att1">" + MyString + "</node>"
“”+MyString+“”
但是,如果.net中有相关内容,为什么要重新发明轮子呢。是否有一种方法可以接受节点名、属性和内部XML并返回XML字符串?

您可以使用

我会给你

<node Attribute1="att1">MyString</node>
MyString
您可以使用

我会给你

<node Attribute1="att1">MyString</node>
MyString

您还可以创建XmlDocument对象并使用其LoadXmlMethod:

XmlDocument document = new XmlDocument();
document.LoadXml("<node Attribute1=\"att1\">" + MyString + "</node>");
XmlDocument document=新的XmlDocument();
document.LoadXml(“+MyString+”);

您还可以创建XmlDocument对象并使用其LoadXmlMethod:

XmlDocument document = new XmlDocument();
document.LoadXml("<node Attribute1=\"att1\">" + MyString + "</node>");
XmlDocument document=新的XmlDocument();
document.LoadXml(“+MyString+”);

cough,cough:XmlTextWriter。但它并不比你的东西好。。。因为开始/结束元素必须手动完成。为什么不用c#框架序列化变量本身呢?咳,咳:XmlTextWriter。但它并不比你的东西好。。。因为开始/结束元素必须手动完成。为什么不用c#框架序列化变量本身呢?