使用LINQ将带分隔符的字符串转换为c#中的xml

时间:2022-07-02 21:49:12

I am using System.Xml and have a textbox with a string delimited by \r\n. The string is basically a list of serial numbers that I want to format as XML to pass to a stored procedure to run a validation.

我用系统。有一个用\r\n分隔的字符串的文本框。该字符串基本上是一个序列号列表,我希望将这些序列号格式化为XML,以便传递给存储过程以运行验证。

An example of the string as it appears now:

现在出现的字符串示例:

"ABC123\r\nDEF456\r\n"

I am thinking it would be a very basic XML Schema, for example

例如,我认为它是一个非常基本的XML模式

<SerialNumbers>
    <Serial>ABC123</Serial>
    <Serial>DEF456</Serial>
<SerialNumbers

What is the basic idea to accomplish this so I have something to springboard off of??

实现这个目的的基本想法是什么?

1 个解决方案

#1


5  

Using LINQ it would be quite simple, after splitting the serials to a list/an array:

使用LINQ,在将序列分割为列表/数组之后,会非常简单:

var doc = new XDocument(
    new XElement("SerialNumbers",
        (from x in serials select new XElement("Serial", x))
    )
);

#1


5  

Using LINQ it would be quite simple, after splitting the serials to a list/an array:

使用LINQ,在将序列分割为列表/数组之后,会非常简单:

var doc = new XDocument(
    new XElement("SerialNumbers",
        (from x in serials select new XElement("Serial", x))
    )
);