帮助检查我的XML验证代码是否有效

时间:2021-11-27 22:14:07

I am checking againtst whether a specific input string are valid (could be used as the value for an XML element) in XML UTF-8 encoding. My goal is to tell which string (from an input string array) is not valid according to XML UTF-8 encoding standard.

我再次检查XML UTF-8编码中的特定输入字符串是否有效(可以用作XML元素的值)。我的目标是根据XML UTF-8编码标准告诉哪个字符串(来自输入字符串数组)无效。

Here is my code, my current implementation is straightforward -- assemble XML file with each individual string from the input string array. I am not sure whether it is the most efficient way. From functional point of view, it works.

这是我的代码,我当前的实现很简单 - 使用输入字符串数组中的每个字符串汇编XML文件。我不确定它是否是最有效的方式。从功能的角度来看,它的工作原理。

My working environment is .Net 3.5 + VSTS 2008 + C#.

我的工作环境是.Net 3.5 + VSTS 2008 + C#。

    static void Main(string[] args)
    {
        string[] inputs = { "Hello", "World", "*", "ServerFault", "&#DFFE" };
        XmlDocument xDoc = new XmlDocument();
        string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        string formatter = "<foo>{0}</foo>";
        foreach (string item in inputs)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append (header);
            builder.Append (String.Format(formatter, item));
            try
            {
                xDoc.Load(new StringReader(builder.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

thanks in advance, George

乔治,提前谢谢

2 个解决方案

#1


George, when writing tests, it's best to start with a test that demonstrates the failure case.

乔治在编写测试时,最好从一个演示失败案例的测试开始。

Will your code ever fail? I don't think so.

你的代码会失败吗?我不这么认为。

You should start with a test that is close to the problem that made you want to create this test. I presume you had a problem with an XML file not being properly encoded? In that case, you should create a test that proves that the bad file is bad (which you already know to be true), then generalize the test so that it can detect other bad files as being bad, and all good files as being good.

您应该从接近导致您想要创建此测试的问题的测试开始。我认为你有一个XML文件没有正确编码的问题?在这种情况下,你应该创建一个测试,证明坏文件是坏的(你已经知道它是真的),然后概括测试,以便它可以检测其他坏文件是坏的,所有好文件是好的。

#2


You could do something like this :

你可以这样做:

    public static XmlElement xmlValidationElement =
        new XmlDocument().CreateElement("validator");

    static void Main(string[] args)
    {
        string[] inputs = { "Hello", "World", "*", "ServerFault" };
        foreach (string item in inputs)
        {
            try
            {
                xmlValidationElement.InnerXml = item;
            }
            catch (XmlException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

#1


George, when writing tests, it's best to start with a test that demonstrates the failure case.

乔治在编写测试时,最好从一个演示失败案例的测试开始。

Will your code ever fail? I don't think so.

你的代码会失败吗?我不这么认为。

You should start with a test that is close to the problem that made you want to create this test. I presume you had a problem with an XML file not being properly encoded? In that case, you should create a test that proves that the bad file is bad (which you already know to be true), then generalize the test so that it can detect other bad files as being bad, and all good files as being good.

您应该从接近导致您想要创建此测试的问题的测试开始。我认为你有一个XML文件没有正确编码的问题?在这种情况下,你应该创建一个测试,证明坏文件是坏的(你已经知道它是真的),然后概括测试,以便它可以检测其他坏文件是坏的,所有好文件是好的。

#2


You could do something like this :

你可以这样做:

    public static XmlElement xmlValidationElement =
        new XmlDocument().CreateElement("validator");

    static void Main(string[] args)
    {
        string[] inputs = { "Hello", "World", "*", "ServerFault" };
        foreach (string item in inputs)
        {
            try
            {
                xmlValidationElement.InnerXml = item;
            }
            catch (XmlException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }