gpt4 book ai didi

c# - 如何在运行时针对 xsd 验证 xml 而无需将 xsd 文件保存在本地文件夹中?

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:33 24 4
gpt4 key购买 nike

我的目标是根据字符串变量中的 xsd 验证 xml 文件。Ps.: 我已经在周五写了一个编号为3072697的问题。但是我今天无法添加这段代码。

书.xml:

<?xml version="1.0" encoding="utf-8"?>
<author xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>

book.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" targetNamespace="urn:bookstore-schema" xmlns:mstns="urn:bookstore-schema" xmlns="urn:bookstore-schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="first-name" type="xs:string" minOccurs="0" />
<xs:element name="last-name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="author" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Xsd_after_saved() 运行完美在 Xsd_after_saved() 中,我需要使用 XSD 的每个地方,我都从本地文件中获取

public void Xsd_after_saved()
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += this.ValidationEventHandler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(@"C:\book.xsd"));
settings.CheckCharacters = true;

XmlReader XmlValidatingReader = XmlReader.Create(@"C:\book.xml", settings);

XmlTextReader Reader = new XmlTextReader(@"C:\book.xml");

StreamReader SR = new StreamReader(@"C:\book.xsd");

XmlSchema Schema = new XmlSchema();

Schema = XmlSchema.Read(SR, ValidationEventHandler);

XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader);

ValidatingReader.ValidationType = ValidationType.Schema;

ValidatingReader.Schemas.Add(Schema);

try
{
XmlValidatingReader.Read();
XmlValidatingReader.Close();

ValidatingReader.ValidationEventHandler += ValidationEventHandler;

while ((ValidatingReader.Read()))
{
}

ValidatingReader.Close();
}
catch (Exception ex)
{
ValidatingReader.Close();
XmlValidatingReader.Close();

}
}

Xsd_whithout_saved() 不工作在 Xsd_whithout_saved() 中,我需要 XSD 的每个地方,我都从名为 readerXsd 的变量 StreamReader 中获得,它来自一个字符串

public void  Xsd_whithout_saved()
{
//>>>Here is the biggest diference from the method Xsd_after_saved: I manipulate the XSD as string because it will come from database and
//it will not allowed to be saved locally
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\book.xsd");
//In the futute, strArquivoInteiro will be fullfill by xsd comming from database as nvarchar(max) and I will not be allowed to save as a file locally
string strArquivoInteiro = xmlDoc.OuterXml;

byte[] byteArray = Encoding.ASCII.GetBytes(strArquivoInteiro);
MemoryStream streamXSD = new MemoryStream(byteArray);
//<<<

StreamReader readerXsd = new StreamReader(streamXSD);

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += this.ValidationEventHandler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(readerXsd));
settings.CheckCharacters = true;

XmlReader XmlValidatingReader = XmlReader.Create(@"C:\book.xml", settings);

XmlTextReader Reader = new XmlTextReader(@"C:\book.xml");

XmlSchema Schema = new XmlSchema();

//IN THIS LINE I RECEIVED THE XmlException "Root Element is Missing" and I can't understand the reason
Schema = XmlSchema.Read(readerXsd, ValidationEventHandler);

XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader);

ValidatingReader.ValidationType = ValidationType.Schema;

ValidatingReader.Schemas.Add(Schema);

try
{

XmlValidatingReader.Read();
XmlValidatingReader.Close();

ValidatingReader.ValidationEventHandler += ValidationEventHandler;

while ((ValidatingReader.Read()))
{

}

ValidatingReader.Close();
}
catch (Exception ex)
{
ValidatingReader.Close();
XmlValidatingReader.Close();
}
}

private void ValidationEventHandler(object sender, ValidationEventArgs args)
{
//place to deal with xml file no valided
}

最佳答案

使用此方法从字符串生成流,(感谢 Cameron MacFarland )

public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}

关于c# - 如何在运行时针对 xsd 验证 xml 而无需将 xsd 文件保存在本地文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3085526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com