-3

I have an XML string from a web service, that might be lacking some child nodes.

Before parsing the XML, i want to check if all the nodes are there and correct.

Example:

<fields>
    <field RandomAttribute="True">  //these attributes should not make a difference
       <name>Test1</name>
       <type>Text</type>
       <id>123</id>
    </field>
    <field>
       <name>Test2</name> //THIS field node lacks the "type" child node
       <id>114</id>
    </field>
</fields>

What is the best way, to take the whole XML, and check whether all the nodes comply to a predefined xml schema/structure?

Notice that I added a RandomAttribute to the first FIELD node, which shouldn't have any effect on the functionality.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Laureant
  • 979
  • 3
  • 18
  • 47

1 Answers1

0

The general way to validate an XML document, is to refer to a XSD schema. The "anyAttribute" element of a schema can be used to validate in the manner that you're asking after, I think.

Have a look at http://www.w3schools.com/xml/schema_complex_anyattribute.asp for an explanation how you can use the anyAttribute type, to accept any well-formed attribute, for a declared element type.

Richard
  • 603
  • 3
  • 14