我创建了一个PowerShell脚本,它循环遍历大量的XML Schema(.xsd)文件,并为每个文件创建一个.NET XmlSchemaSet对象,调用Add()并向Compile()其添加模式,并打印出所有验证错误.
此脚本可以正常工作,但某处存在内存泄漏,如果在100个文件上运行,则会占用数十亿字节的内存.
我在循环中基本上做的是以下内容:
$schemaSet = new-object -typename System.Xml.Schema.XmlSchemaSet
register-objectevent $schemaSet ValidationEventHandler -Action {
...write-host the event details...
}
$reader = [System.Xml.XmlReader]::Create($schemaFileName)
[void] $schemaSet.Add($null_for_dotnet_string, $reader)
$reader.Close()
$schemaSet.Compile()
Run Code Online (Sandbox Code Playgroud)
(可以在此要点中找到重现此问题的完整脚本:https://gist.github.com/3002649.只需运行它,并在任务管理器或Process Explorer中观察内存使用量的增加.)
受到一些博客帖子的启发,我尝试添加
remove-variable reader, schemaSet
Run Code Online (Sandbox Code Playgroud)
我也尝试过$schema从中Add()做起
[void] $schemaSet.RemoveRecursive($schema)
Run Code Online (Sandbox Code Playgroud)
这似乎有一些影响,但仍有泄漏.我假设旧的实例XmlSchemaSet仍在使用内存而不进行垃圾回收.
问题:我如何正确地教垃圾收集器它可以回收上面代码中使用的所有内存?或者更一般地说:如何通过有限的内存来实现我的目标?
我正在尝试使用Xml Schema和XDocument.Validate扩展方法验证Xml片段.每当使用无效的Xml片段时,ValidationEventHandler都会正确触发,但XmlSchemaValidationException的LineNumber和LinePosition属性都为0.
private bool Validate(XDocument doc)
{
bool isValid = true;
List<string> validationErrors = new List<string>();
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, "MyCustomSchema.xsd");
doc.Validate(schemas, (sender, args) =>
{
validationErrors.Add(String.Format("{0}: {1} [Ln {2} Col {3}]",
args.Severity,
args.Exception.Message,
args.Exception.LineNumber,
args.Exception.LinePosition));
isValid = false;
}, false);
return isValid;
}
Run Code Online (Sandbox Code Playgroud)
我在上面的示例中的目标是使用validationErrors通知用户验证失败的原因.但是,使用此方法时,LineNumber和LinePosition都为0.
该片段似乎很简单,并且在验证有效和无效的Xml片段方面似乎按预期工作.
提前致谢!
我在c#中创建了最简单的Web服务:
public void AddData(DataSet ds)
Run Code Online (Sandbox Code Playgroud)
生成的模式(Wsdl)如下所示:
<s:schema xmlns:s="http://www.w3.org/2001/XMLSchema">
...
<s:element ref="s:schema" />
...
</s:schema>
Run Code Online (Sandbox Code Playgroud)
请注意,架构不包含任何import/include元素.
我正在尝试将此架构加载到ac#System.Xml.XmlSchema并将其添加到System.Xml.XmlSchemaSet:
var set = new XmlSchemaSet();
var fs = new FileStream(@"c:\temp\schema.xsd", FileMode.Open);
var s = XmlSchema.Read(fs, null);
set.Add(s);
set.Compile();
Run Code Online (Sandbox Code Playgroud)
最后一行抛出此异常:
The 'http://www.w3.org/2001/XMLSchema:schema' element is not declared.
Run Code Online (Sandbox Code Playgroud)
这有点意义:.Net生成的模式使用"s:schema"类型,该类型在未导入的模式中声明.
使用c#和.net 3.5我正在尝试针对包含的模式验证xml文档.
模式和包括如下
Schema1.xsd - >包含another.xsd
another.xsd - > include base.xsd
当我尝试将Schema1.xsd添加到XmlDocument时,我收到以下错误.
未声明类型"YesNoType"或不是简单类型.
我相信我收到此错误,因为我加载Schema1.xsd架构时没有包含base.xsd文件.
我正在尝试使用XmlSchemaSet类,并且我将XmlResolver uri设置为模式的位置.
注意:所有模式都位于同一目录E:\ Dev\Main\XmlSchemas下
这是代码
string schemaPath = "E:\\Dev\\Main\\XmlSchemas";
XmlDocument xmlDocSchema = new XmlDocument();
XmlSchemaSet s = new XmlSchemaSet();
XmlUrlResolver resolver = new XmlUrlResolver();
Uri baseUri = new Uri(schemaPath);
resolver.ResolveUri(null, schemaPath);
s.XmlResolver = resolver;
s.Add(null, XmlReader.Create(new System.IO.StreamReader(schemaPath + "\\Schema1.xsd"), new XmlReaderSettings { ValidationType = ValidationType.Schema, XmlResolver = resolver }, new Uri(schemaPath).ToString()));
xmlDocSchema.Schemas.Add(s);
ValidationEventHandler valEventHandler = new ValidationEventHandler
(ValidateNinoDobEvent);
try
{
xmlDocSchema.LoadXml(xml);
xmlDocSchema.Validate(valEventHandler);
}
catch (XmlSchemaValidationException xmlValidationError) …Run Code Online (Sandbox Code Playgroud) I have static method, which i use to validate a XML File against a XSD File. This works fine, until there is an XSD File which includes another XSD File.
Example, where i got troubles:
TYPES.XSD:
<xs:simpleType name="MY_AMOUNT">
<xs:restriction base="xs:decimal">
<xs:maxInclusive value="999999999999.99"/>
<xs:minInclusive value="-999999999999.99"/>
<xs:totalDigits value="14"/>
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)
MAIN.XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:include schemaLocation="TYPES.xsd"/>
<xs:element name="ROOT">
<xs:complexType>
<xs:sequence>
<xs:element ref="SOMEREF1"/>
<xs:element ref="SOMEREF2"/>
<xs:element name="AMOUNT" type="MY_AMOUNT" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
VALIDATION CODE:
public static class …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据 Android 设备上的 XSD 验证 XML 文件。
我用谷歌搜索了很多,找到了一些解决方案,例如xerces-for-android。在堆栈溢出中我发现了一些像这样的页面,建议避免javax.xml。验证并为此目的使用 Xerces。
我在不同的地方进行了测试Android APIs(17, 20,25),但不幸的是我没有取得任何成功。
您能帮助我并建议一种替代方法吗?
xmlschemaset ×6
xsd ×5
c# ×3
xml ×3
.net ×2
validation ×2
linq-to-xml ×1
memory-leaks ×1
powershell ×1
schema ×1
xerces ×1
xmldocument ×1