我开发了一个小的C#
脚本,该脚本打开XLS
文件,对其进行解析并创建一个XML
文件的列表,以便与XSD
文件进行验证。XML验证:以空格开头的十进制属性值
我试图上传这些验证的文件到第三方在线服务(同一家公司,给了我的文档/ xsd的东西)和一个生成的文件不被接受,因为不有效。
该文件不被接受,因为它在节点属性中的十进制值的开头处具有空格;删除这个空间解决了这个问题。
我创建了一个简单的测试用例,其中XDocument Validate方法使用额外空间验证XML时没有任何问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Linq;
using System.Xml;
using System.IO;
namespace TestParser {
class Program {
static void Main(string[] args) {
string xsdMarkup =
@"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='option'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base='xs:string'>
<xs:attribute name='value' type='xs:decimal'>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("option","test", new XAttribute("value", " 423423")
));
Console.WriteLine("Validating doc1");
bool errors = false;
doc1.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "not valid" : "validated");
Console.WriteLine();
Console.WriteLine("Contents of doc1:");
Console.WriteLine(doc1);
}
}
}
结果是这样的:
Validating doc1
doc1 validated
Contents of doc1:
<option value=" 423423">test</option>
它是正确的,因为C#XML解析器验证这个XML?
是否有可能强制解析器对这种格式更挑剔?
这是有趣的感谢;第三方采用的解析器似乎根本没有做任何修剪。 – systempuntoout 2011-03-01 15:01:07