使用 XElement select 语句添加许多元素
输入:
"CustomerName Details 121.11.2222 Address-Line1,City,State 36,EU \r
Customer1 SomeDetails 911.911.911 ABCD Street, Some Lane, Some City, Some State 50,USA \n
"
我想生成这样的 XML:
<Customers>
<Customer>
<Name>CustomerName</Name>
<Details>Details</Details>
<Phone>121.11.2222</Phone>
<AddressDetails>
<Address>Address-Line1</Address>
<Address>City</Address>
<Address>State</Address>
</AddressDetails>
<PersonalDetails>
<Age>36</Age>
<Nation>EU</Nation>
</PersonalDetails>
</Customer>
<Customer>
....
</Customer>
</Customers>
好处是所有行集的输入字符串始终遵循相同的格式。
使用 LINQ 尝试类似的操作,当我尝试在同一个选择下创建多个 XElement 时,我陷入了困境(见下文):
string inputString ="CustomerName Details 121.11.2222 Address-Line1,City,State 36,EU \r Customer1 SomeDetails 911.911.911 ABCD Street, Some Lane, Some City, Some State 50,USA \n ";
string[] inputRow = inputString.Split('\n');
var root = new XElement("Customers",
from customerRowSet in inputRow
select new XElement("Customer",
from column in customerRowSet.Split('\t') //Assume columns are tab seperated
select
new XElement("Name", column),
// new XElement("Details", column[1]), Need to add to XML here, but error is thrown on attempt
from commafilters in customerRowSet.Split(',')
select new XElement("AddressDetails", commafilters) //Not sure how to filter out Address[x] seperate from PersonalDetails[y] as both come in comma-seperated
));
还有其他技术可以执行此操作,例如字符串操作或正则表达式吗?
Input:
"CustomerName Details 121.11.2222 Address-Line1,City,State 36,EU \r
Customer1 SomeDetails 911.911.911 ABCD Street, Some Lane, Some City, Some State 50,USA \n
"
I want to generate an XML like this:
<Customers>
<Customer>
<Name>CustomerName</Name>
<Details>Details</Details>
<Phone>121.11.2222</Phone>
<AddressDetails>
<Address>Address-Line1</Address>
<Address>City</Address>
<Address>State</Address>
</AddressDetails>
<PersonalDetails>
<Age>36</Age>
<Nation>EU</Nation>
</PersonalDetails>
</Customer>
<Customer>
....
</Customer>
</Customers>
The good thing is that the input string always follows the same format for all rowsets.
Attempting something like this using LINQ, I get stuck when I try to create more than one XElement under the same select (see below):
string inputString ="CustomerName Details 121.11.2222 Address-Line1,City,State 36,EU \r Customer1 SomeDetails 911.911.911 ABCD Street, Some Lane, Some City, Some State 50,USA \n ";
string[] inputRow = inputString.Split('\n');
var root = new XElement("Customers",
from customerRowSet in inputRow
select new XElement("Customer",
from column in customerRowSet.Split('\t') //Assume columns are tab seperated
select
new XElement("Name", column),
// new XElement("Details", column[1]), Need to add to XML here, but error is thrown on attempt
from commafilters in customerRowSet.Split(',')
select new XElement("AddressDetails", commafilters) //Not sure how to filter out Address[x] seperate from PersonalDetails[y] as both come in comma-seperated
));
Any other techniques to do this like string manipulation or Regex instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不想对列使用 LINQ,因为您希望以不同的方式处理每一列。您可以执行如下操作:
它会生成以下 XML:
You don't want to use LINQ for columns, because you want to treat each column differently. What you can do is something like this:
which produces the following XML: