如何在 C# 中将字节转换为 Xsd 架构



我实际上正在尝试将字节流 Xsd 文件转换为 Xsd 架构,但无法通过以下代码执行此操作

Stream streamXsdd = new MemoryStream(File.ReadAllBytes(path + "\input.xsd"));
XmlSchema xsdDoc = new XmlSchema();
xsdDoc.Write(streamXsdd); OR Read(streamXsdd,validationeventhandler);  
// i thought the above are used for conversion but they are not.
XmlSchemaSet tempSchemaDocs = new XmlSchemaSet();
tempSchemaDocs.Add(xsdDoc);

但上面没有转换它。还有其他方法可以做到吗?

文档建议你想使用XmlSchema.Read.

https://learn.microsoft.com/en-us/dotnet/api/system.xml.schema.xmlschema.read?view=net-6.0#system-xml-schema-xmlschema-read(system-io-stream-system-xml-schema-validationeventhandler)

但是,这是XmlSchema上的静态方法,因此您的代码将更改为更像

Stream streamXsdd = new MemoryStream(File.ReadAllBytes(path + "\input.xsd"));
XmlSchema xsdDoc = XmlSchema.Read(streamXsddd, validationeventhandler);
XmlSchemaSet tempSchemaDocs = new XmlSchemaSet();
tempSchemaDocs.Add(xsdDoc);

可能是您的Read不起作用,因为您有效地丢弃了结果,因为它返回了一个新实例,而不是填充现有实例?

最新更新