序列化的System.Array到XML字符串 [英] Serializing a System.Array to a XML String

查看:186
本文介绍了序列化的System.Array到XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要传递一个字符串数组到SQL Server 2005,所以我写了一个存储过程,而正常accpets一个 XML 参数,并处理它。我的问题是,如果有任何简单的方法来序列化的String [] XML字符串的(在磁盘而不是一个文件)直接在C#无需使用的代码我自己的方法的XDocument,XAttribute 等等。

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.

示例:我希望能够改变像新的String [] {A,b,C} 成类似

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.

推荐答案

您可以尝试 的XmlSerializer 如果你真的想避免编写自己的代码,但与LINQ做它的XML将是简单的:

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();

这篇关于序列化的System.Array到XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆