Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Xml模式控制Xml?_C#_Xml - Fatal编程技术网

C# 如何从Xml模式控制Xml?

C# 如何从Xml模式控制Xml?,c#,xml,C#,Xml,我有一个XSD文件,我想从中检查我的XML。我怎么做?提前感谢 XmlTextWriter objX = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8); objX.WriteStartDocument(); objX.WriteStartElement("MerchantItems"); objX.WriteAttributeString("xmlns", "http://www.si

我有一个XSD文件,我想从中检查我的XML。我怎么做?提前感谢

 XmlTextWriter objX = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);

    objX.WriteStartDocument();

    objX.WriteStartElement("MerchantItems");
    objX.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

我的建议是首先编写整个xml文件。完成后,可以使用XmlSchema类验证创建的xml文档。我不知道在你写文件的过程中怎么做。

这很有帮助

下面是该链接中的一些联合代码,用于加载XML文档并根据关联的模式对其进行验证(您只需确保XML文档正确引用了模式):

使用System.Xml;//对于XmlTextReader和XmlValidatingReader
使用System.Xml.Schema;//对于XmlSchemaCollection(稍后使用)
内部课程计划
{
私人静电
bool isValid=true;//如果发生验证错误,
//在中将此标志设置为false
//验证事件处理程序。
私有静态void Main(字符串[]args)
{
XmlReaderSettings xrs=新的XmlReaderSettings();
xrs.ValidationType=ValidationType.Schema;
xrs.ValidationFlags |=XmlSchemaValidationFlags.ProcessSchemaLocation;
xrs.ValidationFlags |=XmlSchemaValidationFlags.ReportValidationWarnings;
xrs.ValidationEventHandler+=MyValidationEventHandler;
XmlReader r=XmlReader.Create(“,xrs);
while(r.Read())
{
//可以在此处添加代码以处理内容。
}
r、 Close();
//检查文档是否有效。
Console.WriteLine(isValid?“文档有效”:“文档无效”);
Console.In.ReadLine();
}
公共静态无效MyValidationEventHandler(对象发送方,
ValidationEventArgs参数)
{
WriteLine(“验证{1}:{0}”,args.Message,args.Severity);
isValid=false;
}
}

我希望这有帮助

试试这样的东西

这不是一个有效的例子,只是一些让你开始的东西

        using(MemoryStream ms = new MemoryStream())
        using(XmlWriter w = XmlWriter.Create(ms, null))
        {
            // ... WRITE DOCUMENT HERE ...
            XmlDocument x = new XmlDocument();
            x.Load(ms);
            x.Validate(eventHandlerForSchema);
        }

不要使用
新的XmlTextWriter()
。自.NET 2.0以来,它一直被弃用。改为使用
XmlWriter.Create()
。可能是John,但我认为这里的主要问题是他将XML文档的输出直接写入输出流,在文档得到输出之前,他从来没有机会验证文档。你是对的。验证选项位于
XmlReaderSettings
,而不是
XmlWriterSettings
。注释已删除。-1:
不应使用新的XmlTextWriter()
。请改用
XmlWriter.Create()
。没错,我将答案改为使用
XmlWriter.Create()
替代-1:不要使用
新的XmlTextReader()
。使用
XmlReader.Create()
@Joh修复了使用XmlTextReader.Create()的问题!您仍在使用
xmlvalitingreader
,您应该使用
XmlReader.Create
,而不是
XmlTextReader.Create
@John谢谢您的输入。今天我学到了一些东西。我已经更新了我的示例,用XmlReader.Create()代替XmlTextReader.Create(),用XmlReaderSettings代替XmlValidatingReader。
        using(MemoryStream ms = new MemoryStream())
        using(XmlWriter w = XmlWriter.Create(ms, null))
        {
            // ... WRITE DOCUMENT HERE ...
            XmlDocument x = new XmlDocument();
            x.Load(ms);
            x.Validate(eventHandlerForSchema);
        }