【发布时间】:2015-06-23 08:42:06
【问题描述】:
我正在使用以下代码针对 xsd 验证我的 xml。
var isXmlValid = true;
var vinListMessage = "<root xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:test/properties/v1.0\"><test12121 id=\"3\"></test></root>";
var xsdFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "schema.xsd");
var schemas = new XmlSchemaSet();
schemas.Add(null, xsdFilePath);
var xmlDocument = XDocument.Parse(vinListMessage);
xmlDocument.Validate(schemas, (o, e) => { isXmlValid = false; });
Console.WriteLine(isXmlValid);
请注意上述xml中的xmlns,即urn:test/properties/v1.0。
现在在我的 xsd 中,我将 targetnamespace 作为 targetNamespace="urn:testnew/properties/v1.0",这与 xml 中的不同。
现在无论我尝试验证 xsd 的任何 xml,它总是返回 true。但如果我匹配命名空间,那么它工作正常。我想避免对命名空间的依赖。有什么建议吗?
【问题讨论】:
标签: c# xsd linq-to-xml