XmlReader 测试为换行符返回的字符串
XmlReader testing of string returned for newline
我正在编写 C# class 用于生成电子邮件列表,以便在进程成功或失败时使用。在 运行 通过来自网络的 XmlReader 示例中,我发现验证 Read() 比看起来更难。
我可以使用 string.IsNullOrEmpty(x) 来测试空值或空节点,但它仍然会在 x 的工具提示中显示“\n”的测试失败。测试“\n”、“\n”、“\n”。 "\n" 或 char(13) 均失败。如果我使用 x.Contains((char)13),它总能找到它并进入尝试构建电子邮件地址列表的代码。到目前为止,它要么总是失败,要么总是成功。
我在 Whosebug 上发现了一些旧帖子,其中的问题似乎是相同的,但我的结果与答案不匹配。我的环境是 Windows 8.1 运行 Visual Studio 2013 with .Net Framework 4.51。在我的 class 中使用解决方案之前,我试图在网上工作的示例位于 Microsoft.com
我的转换如下:
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Collections.Generic;
namespace XMLDemo
{
public class project
{
public static void Main()
{
string uri = @"C:\events\items.xml";
string process_state = "Item";
string emails = StreamEmailAddress(uri, process_state);
}
private static string StreamEmailAddress(string uri, string process_state)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader reader = XmlReader.Create(uri, settings);
string returnValue = "";
reader.MoveToContent();
while (reader.Read())
{
string x = reader.Value;
if ((string.IsNullOrEmpty(x) == false) && (x.Contains((char)13)))
{
returnValue = returnValue + x + "; ";
}
}
Console.WriteLine("Made it to the end: " + returnValue);
return returnValue;
}
}
}
你应该使用string.IsNullOrWhiteSpace
已解决:经过一整天的摸索,我在发帖后用稍微新鲜的眼光看了看它,发现了明显的错误。我使用了不正确的功能并试图自己解决换行问题。通过用 IsNullOrWhiteSpace(x) 替换 IsNullOrEmpty(x),我得到了预期的数据字符串。现在用电子邮件地址编写代码会很容易。
我正在编写 C# class 用于生成电子邮件列表,以便在进程成功或失败时使用。在 运行 通过来自网络的 XmlReader 示例中,我发现验证 Read() 比看起来更难。
我可以使用 string.IsNullOrEmpty(x) 来测试空值或空节点,但它仍然会在 x 的工具提示中显示“\n”的测试失败。测试“\n”、“\n”、“\n”。 "\n" 或 char(13) 均失败。如果我使用 x.Contains((char)13),它总能找到它并进入尝试构建电子邮件地址列表的代码。到目前为止,它要么总是失败,要么总是成功。
我在 Whosebug 上发现了一些旧帖子,其中的问题似乎是相同的,但我的结果与答案不匹配。我的环境是 Windows 8.1 运行 Visual Studio 2013 with .Net Framework 4.51。在我的 class 中使用解决方案之前,我试图在网上工作的示例位于 Microsoft.com
我的转换如下:
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Collections.Generic;
namespace XMLDemo
{
public class project
{
public static void Main()
{
string uri = @"C:\events\items.xml";
string process_state = "Item";
string emails = StreamEmailAddress(uri, process_state);
}
private static string StreamEmailAddress(string uri, string process_state)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader reader = XmlReader.Create(uri, settings);
string returnValue = "";
reader.MoveToContent();
while (reader.Read())
{
string x = reader.Value;
if ((string.IsNullOrEmpty(x) == false) && (x.Contains((char)13)))
{
returnValue = returnValue + x + "; ";
}
}
Console.WriteLine("Made it to the end: " + returnValue);
return returnValue;
}
}
}
你应该使用string.IsNullOrWhiteSpace
已解决:经过一整天的摸索,我在发帖后用稍微新鲜的眼光看了看它,发现了明显的错误。我使用了不正确的功能并试图自己解决换行问题。通过用 IsNullOrWhiteSpace(x) 替换 IsNullOrEmpty(x),我得到了预期的数据字符串。现在用电子邮件地址编写代码会很容易。