如何转换NameValueCollection,如:
var mydata = new NameValueCollection();
mydata.Add("UserID", "3698521478963");
mydata.Add("Password", "23584");
mydata.Add("VerifyCode", "23654");
到这样的XML字符串:
XML数据:
答案 0 :(得分:3)
您可以使用LINQ-to-XML来实现这一目标,例如:
var mydata = new NameValueCollection();
mydata.Add("UserID", "3698521478963");
mydata.Add("Password", "23584");
mydata.Add("VerifyCode", "23654");
var result = new XElement("Root",
mydata.AllKeys.Select(o => new XElement(o, mydata[o]))
);
Console.WriteLine(result.ToString());
<强> Dotnetfiddle Demo
强>
输出
<Root>
<UserID>3698521478963</UserID>
<Password>23584</Password>
<VerifyCode>23654</VerifyCode>
</Root>