Xdocument 尝试创建 XML 文件,但在使用 ListBox 时遇到问题
因此,我决定使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LINQ to XML 在这方面确实非常好 - 如果您向它提供可迭代的内容片段,它就会进行迭代。试试这个:
请注意,我已将
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:
Note that I've changed
SelectedValue
toSelectedValues
to get multiple values. If that's not what you want, you should hopefully be able to adjust it accordingly.