Xml字符串反序列化的一行程序

时间:2022-09-28 22:32:16

Following my question on serializing a System.Array to a Xml string, I would like to ask if anyone knows of a one-line instruction to go the other way round, that is, to convert a Xml string such as

下面是我关于序列化系统的问题。将数组转换为Xml字符串,我想问,是否有人知道用一行指令反过来转换Xml字符串,例如

<Root><Element>a</Element><Element>b</Element></Root>

元素 <根> < > < / >元素b <元素> < /元素>根> < /

to a new string[] { "a", "b" } object. I suspect that using String.Split would be enough for the case, but it doesn't seem like the most elegant solution, does it?

到一个新的字符串[]{"a", "b"}对象。我怀疑是用字符串。拆分对于这种情况来说已经足够了,但它似乎不是最优雅的解决方案,不是吗?

2 个解决方案

#1


3  

How about

如何

var data = XElement.Parse("<Root><Element>a</Element><Element>b</Element></Root>").Elements("Element").Select(e=>e.Value).ToArray();

I would advise making this more than one line for readability.

为了可读性,我建议多写一行。

#2


2  

string test = "<Root><Element>a</Element><Element>b</Element></Root>";
var results = XElement.Parse(test).Elements("Element").Select(e => e.Value).ToArray();
  1. Parse the string
  2. 解析字符串
  3. Select the elements called "Element"
  4. 选择称为“元素”的元素
  5. Select the value in the elements
  6. 选择元素中的值
  7. Convert to array.
  8. 转换为数组。
  9. (Optionally)Format it so it's one line.
  10. (可选)格式化为一行。

#1


3  

How about

如何

var data = XElement.Parse("<Root><Element>a</Element><Element>b</Element></Root>").Elements("Element").Select(e=>e.Value).ToArray();

I would advise making this more than one line for readability.

为了可读性,我建议多写一行。

#2


2  

string test = "<Root><Element>a</Element><Element>b</Element></Root>";
var results = XElement.Parse(test).Elements("Element").Select(e => e.Value).ToArray();
  1. Parse the string
  2. 解析字符串
  3. Select the elements called "Element"
  4. 选择称为“元素”的元素
  5. Select the value in the elements
  6. 选择元素中的值
  7. Convert to array.
  8. 转换为数组。
  9. (Optionally)Format it so it's one line.
  10. (可选)格式化为一行。