Xdocument 尝试创建 XML 文件,但在使用 ListBox 时遇到问题

发布于 2024-09-12 04:01:37 字数 1144 浏览 6 评论 0原文

因此,我决定使用 XDocument 创建一个 XML 文件,该文件运行良好,直到我遇到必须在 ListBox 中查找所有选定项目的部分。我不确定应该如何格式化它。

     XDocument xmlDoc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("Created: " + DateTime.Now.ToString()),
                    new XElement("Trip",
                        new XElement("TripDetails",
                            new XElement("Departure", txtDeparture.Text),
                            new XElement("Return", txtReturn.Text),                     
                             new XElement("Purpose", txtPurpose.Text),
                             new XElement("Region", ddlRegion.SelectedValue.ToString()),
  //Not working                            
                             new XElement("Countries", 
                             foreach(string x in lstCountry.SelectedValue)
                             {
                                 new XElement("Country",x);
                             }
                            )
                          )
                        )  
                       );

我想在“国家”下的子节点中输出每个选定的国家

So I have decided to use XDocument to create a XML file, which was working great until I came across a part where I have to find all the selected items in a ListBox. I am unsure how I should format this.

     XDocument xmlDoc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("Created: " + DateTime.Now.ToString()),
                    new XElement("Trip",
                        new XElement("TripDetails",
                            new XElement("Departure", txtDeparture.Text),
                            new XElement("Return", txtReturn.Text),                     
                             new XElement("Purpose", txtPurpose.Text),
                             new XElement("Region", ddlRegion.SelectedValue.ToString()),
  //Not working                            
                             new XElement("Countries", 
                             foreach(string x in lstCountry.SelectedValue)
                             {
                                 new XElement("Country",x);
                             }
                            )
                          )
                        )  
                       );

I want to output each selected country in child nodes under Countries

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ゝ杯具 2024-09-19 04:01:37

LINQ to XML 在这方面确实非常好 - 如果您向它提供可迭代的内容片段,它就会进行迭代。试试这个:

XDocument xmlDoc = new XDocument(
       new XDeclaration("1.0", "utf-8", "yes"),
       new XComment("Created: " + DateTime.Now.ToString()),
       new XElement("Trip",
           new XElement("TripDetails",
               new XElement("Departure", txtDeparture.Text),
               new XElement("Return", txtReturn.Text),                     
               new XElement("Purpose", txtPurpose.Text),
               new XElement("Region", ddlRegion.SelectedValue.ToString()),
               new XElement("Countries",
                    lstCountry.SelectedValues
                              .Cast<string>()
                              .Select(x => new XElement("Country", x))
              )
          )  
      );

请注意,我已将 SelectedValue 更改为 SelectedValues 以获取多个值。如果这不是您想要的,那么您应该能够进行相应的调整。

LINQ to XML is really nice in this respect - if you provide it with an iterable piece of content, it will iterate. Try this:

XDocument xmlDoc = new XDocument(
       new XDeclaration("1.0", "utf-8", "yes"),
       new XComment("Created: " + DateTime.Now.ToString()),
       new XElement("Trip",
           new XElement("TripDetails",
               new XElement("Departure", txtDeparture.Text),
               new XElement("Return", txtReturn.Text),                     
               new XElement("Purpose", txtPurpose.Text),
               new XElement("Region", ddlRegion.SelectedValue.ToString()),
               new XElement("Countries",
                    lstCountry.SelectedValues
                              .Cast<string>()
                              .Select(x => new XElement("Country", x))
              )
          )  
      );

Note that I've changed SelectedValue to SelectedValues to get multiple values. If that's not what you want, you should hopefully be able to adjust it accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文