What is the best way to verify the validity of an XML method?

I have some WCF methods that are used to transfer information from a server application to the site interface for use in binding. I am posting the result as an XElement, which is the root of the XML tree containing the data I want to bind.

I would like to create some tests that examine the data and ensure that this happens as expected.

My current thinking is this: every method that returns an XElement tree has a corresponding schema file (.XSD). This file is included in the assembly, which contains my WCF classes as an embedded resource.

Tests call the method of these methods and compare the result with these built-in circuits.

Is that a good idea? If not, what other methods can I use to provide a “guarantee” of which XML method will return?

If so, how do you check for an XElement on a schema? And how can I get this circuit from its built-in assembly?

+5
source share
2 answers

Id says that proper xml validation using the xsd schema is a good idea.

How to check an XElement with a loaded schema: As you can see in this example, you need to check the XDocument first to fill in the "information after the schema" (there might be a solution for this without using the Validate method on XDOcument, but Im still haven't found it):

String xsd =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
   <xsd:element name='root'>
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name='child1' minOccurs='1' maxOccurs='1'>
       <xsd:complexType>
        <xsd:sequence>
         <xsd:element name='grandchild1' minOccurs='1' maxOccurs='1'/>
         <xsd:element name='grandchild2' minOccurs='1' maxOccurs='2'/>
        </xsd:sequence>
       </xsd:complexType>
      </xsd:element>
     </xsd:sequence>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>";
String xml = @"<?xml version='1.0'?>
<root>
    <child1>
        <grandchild1>alpha</grandchild1>
        <grandchild2>beta</grandchild2>
    </child1>
</root>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsd)));
XDocument doc = XDocument.Load(XmlReader.Create(new StringReader(xml)));
Boolean errors = false;
doc.Validate(schemas, (sender, e) =>
{
    Console.WriteLine(e.Message);
    errors = true;
}, true);
errors = false;
XElement child = doc.Element("root").Element("child1");
child.Validate(child.GetSchemaInfo().SchemaElement, schemas, (sender, e) =>
{
    Console.WriteLine(e.Message);
    errors = true;
});

XmlSchemaSet:

Assembly assembly = Assembly.GetExecutingAssembly();
// you can use reflector to get the full namespace of your embedded resource here
Stream stream = assembly.GetManifestResourceStream("AssemblyRootNamespace.Resources.XMLSchema.xsd");
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, XmlReader.Create(stream));
+11

, XSD , XML-. , , XElement. - ExceptionXElement, - HttpHeaderXElement .. XElement Parse TryParse, , XML-, . TryParse() false, XML, ( , ..).

:

public class MyXElement : XElement 
{

    public MyXElement(XElement element)
        : base(element)
    { }

    public static bool TryParse(string xml, out MyXElement myElement)
    {
        XElement xmlAsXElement;

        try
        {
            xmlAsXElement = XElement.Parse(xml);
        }
        catch (XmlException)
        {
            myElement = null;
            return false;
        }

        // Use LINQ to check if xmlAsElement has correct nodes...
    }
+4

All Articles