使用 XElement select 语句添加许多元素

发布于 2024-11-08 14:57:53 字数 1741 浏览 0 评论 0原文

输入:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-11-15 14:57:54

您不想对列使用 LINQ,因为您希望以不同的方式处理每一列。您可以执行如下操作:

var root = new XElement("Customers",
    from customerRowSet in inputRow
    let columns = customerRowSet.Split('\t') //Assume columns are tab seperated
    select new XElement("Customer",
        new XElement("Name", columns[0]),
        new XElement("Details", columns[1]),
        new XElement("Phone", columns[2]),
        new XElement("Address",
            from commafilters in columns[3].Split(',')
            select new XElement("AddressDetails", commafilters.TrimStart())
    )));

它会生成以下 XML:

<Customers>
  <Customer>
    <Name>CustomerName</Name>
    <Details>Details</Details>
    <Phone>121.11.2222</Phone>
    <Address>
      <AddressDetails>Address-Line1</AddressDetails>
      <AddressDetails>City</AddressDetails>
      <AddressDetails>State</AddressDetails>
    </Address>
  </Customer>
  <Customer>
    <Name>Customer1</Name>
    <Details>SomeDetails</Details>
    <Phone>911.911.911</Phone>
    <Address>
      <AddressDetails>ABCD Street</AddressDetails>
      <AddressDetails>Some Lane</AddressDetails>
      <AddressDetails>Some City</AddressDetails>
      <AddressDetails>Some State</AddressDetails>
    </Address>
  </Customer>
</Customers>

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:

var root = new XElement("Customers",
    from customerRowSet in inputRow
    let columns = customerRowSet.Split('\t') //Assume columns are tab seperated
    select new XElement("Customer",
        new XElement("Name", columns[0]),
        new XElement("Details", columns[1]),
        new XElement("Phone", columns[2]),
        new XElement("Address",
            from commafilters in columns[3].Split(',')
            select new XElement("AddressDetails", commafilters.TrimStart())
    )));

which produces the following XML:

<Customers>
  <Customer>
    <Name>CustomerName</Name>
    <Details>Details</Details>
    <Phone>121.11.2222</Phone>
    <Address>
      <AddressDetails>Address-Line1</AddressDetails>
      <AddressDetails>City</AddressDetails>
      <AddressDetails>State</AddressDetails>
    </Address>
  </Customer>
  <Customer>
    <Name>Customer1</Name>
    <Details>SomeDetails</Details>
    <Phone>911.911.911</Phone>
    <Address>
      <AddressDetails>ABCD Street</AddressDetails>
      <AddressDetails>Some Lane</AddressDetails>
      <AddressDetails>Some City</AddressDetails>
      <AddressDetails>Some State</AddressDetails>
    </Address>
  </Customer>
</Customers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文