本文整理汇总了C#中System.Xml.Linq.XDocument.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# XDocument.Validate方法的具体用法?C# XDocument.Validate怎么用?C# XDocument.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XDocument
的用法示例。
在下文中一共展示了XDocument.Validate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetXml
public string GetXml(bool validate)
{
XNamespace ns = "http://sd.ic.gc.ca/SLDR_Schema_Definition_en";
var spectrum_licences = getLicences();
XDocument doc = new XDocument(new XElement("spectrum_licence_data_registry", spectrum_licences));
foreach (XElement e in doc.Root.DescendantsAndSelf())
{
if (e.Name.Namespace == "")
e.Name = ns + e.Name.LocalName;
}
var errors = new StringBuilder();
if (validate)
{
XmlSchemaSet set = new XmlSchemaSet();
var schema = getMainSchema();
set.Add(null, schema);
doc.Validate(set, (sender, args) => { errors.AppendLine(args.Message); });
}
//return errors.Length > 0 ? errors.ToString() : doc.ToString();
var result = errors.Length > 0 ? "Validation Errors: " + errors.ToString() : getDocumentAsString(doc);
return result;
}
示例2: Parse
public List<EndpointInstance> Parse(XDocument document)
{
document.Validate(schema, null, true);
var root = document.Root;
var endpointElements = root.Descendants("endpoint");
var instances = new List<EndpointInstance>();
foreach (var e in endpointElements)
{
var endpointName = e.Attribute("name").Value;
foreach (var i in e.Descendants("instance"))
{
var discriminatorAttribute = i.Attribute("discriminator");
var discriminator = discriminatorAttribute?.Value;
var properties = i.Attributes().Where(a => a.Name != "discriminator");
var propertyDictionary = properties.ToDictionary(a => a.Name.LocalName, a => a.Value);
instances.Add(new EndpointInstance(endpointName, discriminator, propertyDictionary));
}
}
return instances;
}
示例3: DDEX
public DDEX(XDocument doc, bool verifySchema = false)
{
if (verifySchema)
doc.Validate(DDEXSchema.GetDdexSchema(), null);
Root = new NewReleaseMessage(doc.Root);
}
示例4: Validate
private static void Validate(XDocument doc, XmlSchemaSet xmlScheme)
{
bool notValidated = false;
doc.Validate(xmlScheme, (eventObject, eventHandler) => { notValidated = true; }, false);
Console.WriteLine(notValidated ? "Not Validated" : "Validated");
}
示例5: IsValidMessage
internal static bool IsValidMessage(XDocument doc, XDocument schema)
{
var schemas = new XmlSchemaSet();
schemas.Add("http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message",
GetPath("lib\\SDMXMessage.xsd"));
if (schema != null)
{
using (var reader = schema.CreateReader())
{
schemas.Add(null, reader);
}
}
bool isValid = true;
doc.Validate(schemas, (s, args) =>
{
isValid = false;
if (args.Severity == XmlSeverityType.Warning)
Console.WriteLine("\tWarning: " + args.Message);
else
Console.WriteLine("\tError: " + args.Message);
});
return isValid;
}
示例6: ValidateSchema
private static void ValidateSchema(XDocument doc, XmlSchemaSet schema, string file)
{
doc.Validate(schema, (obj, ev) =>
{
Console.WriteLine("{0} --> {1}", file, ev.Message);
});
}
示例7: ValidateSchema
private static void ValidateSchema(XDocument doc, XmlSchemaSet schema, string file)
{
doc.Validate(schema, (o, e) =>
{
Console.WriteLine("Not valid schema in file \"{0}\"\nException: {1}", file, e.Message);
});
}
示例8: Normalize
public static XDocument Normalize(XDocument source, XmlSchemaSet schema)
{
bool havePSVI = false;
// validate, throw errors, add PSVI information
if (schema != null)
{
source.Validate(schema, null, true);
havePSVI = true;
}
return new XDocument(
source.Declaration,
source.Nodes().Select(n =>
{
// Remove comments, processing instructions, and text nodes that are
// children of XDocument. Only white space text nodes are allowed as
// children of a document, so we can remove all text nodes.
if (n is XComment || n is XProcessingInstruction || n is XText)
return null;
XElement e = n as XElement;
if (e != null)
return NormalizeElement(e, havePSVI);
return n;
}
)
);
}
示例9: Validate
/// <summary>
/// Validates Xml against the configured schema and outs validation erros if any
/// </summary>
/// <param name="xmlDoc"></param>
/// <param name="validationErrors"></param>
/// <returns></returns>
public bool Validate(XDocument xmlDoc, out string validationErrors)
{
validationErrors = null;
//This will contain the xml schema
var schemas = new XmlSchemaSet();
//Add the schema, targetNamespace not specified
schemas.Add(_xmlNamespace, XmlReader.Create(new StringReader(_xsdDoc)));
//Variable to check if there are errors
var hasValidationErrors = false;
string xmlValidationErrorMessage = null;
//Validate the xml against the schema
xmlDoc.Validate(schemas, (sender, eventArgs) =>
{
hasValidationErrors = true;
xmlValidationErrorMessage = eventArgs.Message;
});
validationErrors = xmlValidationErrorMessage;
// validation fails if validation errors
return !hasValidationErrors;
}
示例10: PrintValidationResult
private static void PrintValidationResult(XDocument doc, XmlSchemaSet schema, string file)
{
doc.Validate(schema, (obj, ev) =>
{
Console.WriteLine("* {0} * {1}",file, ev.Message);
});
}
示例11: PrintValidationResult
private static void PrintValidationResult(XDocument doc, XmlSchemaSet schema, string file)
{
doc.Validate(schema, (obj, ev) =>
{
Console.WriteLine("!!! SCHEME VALIDATION ERROR IN FILE ---> {0} !!! {1}", file, ev.Message);
});
}
示例12: ValidateSave
public void ValidateSave()
{
WorldEntity world = createTestWorld();
XElement xElement = WorldTransformer.Instance.ToXElement(world, TransformerSettings.WorldNamespace + "World");
xElement.Save("unittestsave.xml", SaveOptions.OmitDuplicateNamespaces);
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "WorldSchema1.xsd");
XDocument xDocument = new XDocument(xElement);
xDocument.AddAnnotation(SaveOptions.OmitDuplicateNamespaces);
xDocument.Save("unittest2.xml", SaveOptions.OmitDuplicateNamespaces);
XElement x1 = new XElement(TransformerSettings.WorldNamespace + "Outer");
x1.Add(new XElement(TransformerSettings.WorldNamespace + "Inner"));
x1.Save("unittest3.xml", SaveOptions.OmitDuplicateNamespaces);
string val = "";
xDocument.Validate(schemaSet, (o, vea) => {
val += o.GetType().Name + "\n";
val += vea.Message + "\n";
}, true);
Assert.AreEqual("", val);
}
示例13: ValidateXml
private static void ValidateXml(XDocument document, XmlSchemaSet schema)
{
document.Validate(schema, (obj, ev) =>
{
Console.WriteLine("{0}", ev.Message);
});
}
示例14: IsValid
public bool IsValid(XDocument doc, XDocument schema, string xmlns = "")
{
var isValid = true;
if (string.IsNullOrEmpty(xmlns))
{
xmlns = GetXmlnsFromSchema(schema);
}
// 1. Verify that the document has a namespace
if (!string.IsNullOrEmpty(xmlns) && string.IsNullOrEmpty(GetXmlnsFromDocument(doc)))
{
return false;
}
// 2. Validate against schema
var schemas = new XmlSchemaSet();
schemas.Add(xmlns, XmlReader.Create(new StringReader(schema.ToString())));
doc.Validate(schemas, (sender, args) =>
{
isValid = false;
}, true);
return isValid;
}
示例15: IsValidTwiML
public bool IsValidTwiML(XDocument doc)
{
Console.Write(doc.ToString());
var valid = true;
doc.Validate(Schemas, (o, e) => { valid = false; });
return valid;
}