在C#中循环遍历XML文件时,如何用字符分隔结果?

时间:2022-10-19 13:52:19

I am storing some settings in a settings.xml file for my C# Windows Forms Application and in that XML file I am storing e-mail addresses.

我将一些设置存储在我的C#Windows窗体应用程序的settings.xml文件中,并在该XML文件中存储电子邮件地址。

I would ultimately like to achieve being able to loop through these e-mail addresses and send one e-mail to all of them.

我最终希望能够遍历这些电子邮件地址并向所有人发送一封电子邮件。

What would be the best way of looping through them and adding them using the To.Add method of the MailMessage class in C#?

循环使用它们并使用C#中MailMessage类的To.Add方法添加它们的最佳方法是什么?

I already have the following code below to retrieve them from the XML file:

我已经有以下代码从XML文件中检索它们:

var doc = XDocument.Load(Application.StartupPath + "//settings.xml");
StringBuilder result = new StringBuilder();
foreach (XElement c in doc.Descendants("EmailAddresses")) 
{
    MessageBox.Show("Results: " + c.Value, "Test");
}       

I have not been able to figure out how to split the results. The results in the MessageBox are like so: "email@domain.comemail@domain.comemail@domain.com" and so on..or even if this is the best way to achieve what I want.

我无法弄清楚如何分割结果。 MessageBox中的结果如下:“email @ domain.comemail @ domain.comemail @ domain.com”等等。即使这是实现我想要的最佳方式。

Your help is greatly appreciated!

非常感谢您的帮助!

1 个解决方案

#1


0  

Rather than evaluating the entire content below the "EmailAddresses" element, you should be enumerating its child nodes individually. Assuming the "Email<#>" elements are the only children, code similar to what commenter stribizhev offered should work fine:

您应该单独枚举其子节点,而不是评估“EmailAddresses”元素下面的整个内容。假设“Email <#>”元素是唯一的子元素,类似于stribizhev提供的代码应该可以正常工作:

foreach(XElement c in doc.Descendants("EmailAddresses")
    .SelectMany(x => x.DescendantNodes()
        .Where(‌​x => x.NodeType == System.Xml.XmlNodeType.Text)))
{
    MessageBox.Show("Results: " + c.Value, "Test");
}

Note that you can't actually call DescendantNodes() on the result of the call to Descendants(), as that return value is an instance of IEnumerable<XElement>, not a single XElement. But you can use the SelectMany() method to flatten the enumeration of descendants into an enumeration of their descendants.

请注意,您无法在调用Descendants()的结果上调用DescendantNodes(),因为该返回值是IEnumerable 的实例,而不是单个XElement。但是您可以使用SelectMany()方法将后代的枚举展平为其后代的枚举。

Alternatively, you could check the node's name:

或者,您可以检查节点的名称:

foreach(XElement c in doc.Descendants("EmailAddresses")
    .SelectMany(x => x.Elements().Where(‌​x => x.Name.StartsWith("Email")))
{
    MessageBox.Show("Results: " + c.Value, "Test");
}

Based on the information you've provided so far, I would expect either of those to work fine.

根据您目前提供的信息,我希望其中任何一个都可以正常工作。

The above just displays the values in the MessageBox, as in your original example. Obviously, you can just pass c.Value to the MailAddressCollection.Add() method instead, to add them as you wanted.

上面只显示MessageBox中的值,如原始示例中所示。显然,您可以将c.Value传递给MailAddressCollection.Add()方法,以便根据需要添加它们。

#1


0  

Rather than evaluating the entire content below the "EmailAddresses" element, you should be enumerating its child nodes individually. Assuming the "Email<#>" elements are the only children, code similar to what commenter stribizhev offered should work fine:

您应该单独枚举其子节点,而不是评估“EmailAddresses”元素下面的整个内容。假设“Email <#>”元素是唯一的子元素,类似于stribizhev提供的代码应该可以正常工作:

foreach(XElement c in doc.Descendants("EmailAddresses")
    .SelectMany(x => x.DescendantNodes()
        .Where(‌​x => x.NodeType == System.Xml.XmlNodeType.Text)))
{
    MessageBox.Show("Results: " + c.Value, "Test");
}

Note that you can't actually call DescendantNodes() on the result of the call to Descendants(), as that return value is an instance of IEnumerable<XElement>, not a single XElement. But you can use the SelectMany() method to flatten the enumeration of descendants into an enumeration of their descendants.

请注意,您无法在调用Descendants()的结果上调用DescendantNodes(),因为该返回值是IEnumerable 的实例,而不是单个XElement。但是您可以使用SelectMany()方法将后代的枚举展平为其后代的枚举。

Alternatively, you could check the node's name:

或者,您可以检查节点的名称:

foreach(XElement c in doc.Descendants("EmailAddresses")
    .SelectMany(x => x.Elements().Where(‌​x => x.Name.StartsWith("Email")))
{
    MessageBox.Show("Results: " + c.Value, "Test");
}

Based on the information you've provided so far, I would expect either of those to work fine.

根据您目前提供的信息,我希望其中任何一个都可以正常工作。

The above just displays the values in the MessageBox, as in your original example. Obviously, you can just pass c.Value to the MailAddressCollection.Add() method instead, to add them as you wanted.

上面只显示MessageBox中的值,如原始示例中所示。显然,您可以将c.Value传递给MailAddressCollection.Add()方法,以便根据需要添加它们。