Die XDocument -Validierung löst eine Ausnahme auf ## Andere ausC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Die XDocument -Validierung löst eine Ausnahme auf ## Andere aus

Post by Anonymous »

Ich versuche, XML gegen XSD -Dateien mit einer Validierung mit XDocument zu validieren, aber es fällt in diesem Element, das ## Other bezeichnet wird, ständig fehl. Ich habe versucht, XML für diesen XSD hier zu generieren. Haben Sie eine Idee, was ich tun kann, um es zu beheben? Ich kann XSD -Dateien leider nicht ändern. < /P>
Beispiel xsd: < /p>

Code: Select all












< /code>
C# Code zum Testen < /p>
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml;
static XmlSchemaValidationException CreateSchemaValidationException(object o, ValidationEventArgs e)
{
StringBuilder sb = new StringBuilder();
if (o is XElement)
{
sb.AppendLine($"Node Name: {((XElement)o).Name}");
}
if (o is XAttribute)
{
sb.AppendLine($"Attribute Name: {((XAttribute)o).Name}");
}
if (o is IXmlLineInfo)
{
var lineinfo = (IXmlLineInfo)o;
if (lineinfo.HasLineInfo())
{
sb.AppendLine($"Line Number: {lineinfo.LineNumber}");
sb.AppendLine($"Line Position: {lineinfo.LinePosition}");
}
}
sb.AppendLine(e.Message);
return new XmlSchemaValidationException(sb.ToString());
}

static XDocument ParseWithOptions(string xml)
=> XDocument.Parse(xml, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo | LoadOptions.SetBaseUri);

static XmlSchemaSet LoadSchemaSet(string xsd)
{
var schemaSet = new XmlSchemaSet()
{
XmlResolver = null,
};
{
var read = XmlReader.Create(new StringReader(xsd), new XmlReaderSettings()
{
DtdProcessing = DtdProcessing.Ignore,
IgnoreWhitespace = true,
IgnoreComments = true,
XmlResolver = null,

});
schemaSet.Add(null, read);
}

schemaSet.Compile();
return schemaSet;
}

string xml = "\r\n\r\n";

string xsd = "\r\n\r\n\t\r\n\t\r\n\t\t\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\r\n";

//throw new Exception();
ParseWithOptions(xml).Validate(LoadSchemaSet(xsd), (o, e) =>
{
throw CreateSchemaValidationException(o, e);
});

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post