Serializing a System.Array to a XML String

自古美人都是妖i 提交于 2020-01-13 10:22:37

问题


I need to pass an array of strings to SQL Server 2005, and so I wrote a stored procedure which accpets a XML parameter and deals with it properly. My question is if there is any easy way to serialize a string[] to a XML string (not a file in the disk) directly in C# without having to code my own method using XDocument, XAttribute and the like.

Example: I want to be able to transform something like new string[] { "a", "b", "c" } into something like

<StringList><String>a</String><String>b</String><String>c</String></StringList>

Element tag names are unimportant.


回答1:


You could try XmlSerializer if you really want to avoid writing your own code, but doing it with LINQ to XML would be as simple as:

XElement element = new XElement("StringList",
                                values.Select(x => new XElement("String", x)));
string text = element.ToString();


来源:https://stackoverflow.com/questions/6330148/serializing-a-system-array-to-a-xml-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!