vb.net: I have a task to take the given xsd file and use that to validate my generated xml file. But now this xsd file has complex type attributes and I cant get to append this complex type to my declared xmlnode.

My Code:

Dim namespaceURI As String = ""
Dim odoc As New XmlDocument
Dim aschema As XmlSchema = XmlSchema.Read(New FileStream(xsdFilePath, FileMode.Open), Nothing)
            odoc.Schemas.Add(aschema)
 Dim oRoot As XmlNode = odoc.CreateNode(XmlNodeType.Element, "RootName", namespaceURI) 
odoc.AppendChild(oRoot)
...
...
' HERES MY PROBLEM
Dim oSubRoot As XmlNode = odoc.CreateNode(XmlNodeType.Element, "SubRootName", namespaceURI)
oRoot.AppendChild(oSubRoot)

This oSubRoot has a Complex Type "Person" ~ I then create this Persons Class but how do I append this class to the oSubRoot node? When I run my code I get this error on my xml file validation
Error: The element '' is abstract or its type is abstract.

I am still new in xml and would like to know what is the best practice for achieving this task.

Dani AI

Generated

Short diagnosis: that validation error means the schema does not accept the element you created directly — the XSD defines an abstract head or expects a concrete substitute/derived instance. In practice you either need to produce the concrete element that belongs to the substitution group, or mark the instance with a concrete type (xsi:type) so the validator knows which concrete type to use.

Two practical approaches that work in .NET

  • Manual DOM fix (when you want to keep building XmlDocument by hand). Make sure the concrete element name / namespace from the XSD is used, or add an xsi:type that points to a concrete type and declare the corresponding prefix on the document root. Example (VB.NET):
' ensure namespace prefix for the concrete type is declared on the root
rootNode.SetAttribute("xmlns:tns", "http://www.blabla.za")

' add xsi:type so the validator sees a concrete type
Dim xsiType As XmlAttribute = xmlDoc.CreateAttribute("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance")
xsiType.Value = "tns:PersonType"   ' prefix:TypeName must match the schema
subNode.Attributes.Append(xsiType)
  • Generate types + serialize (recommended for non-trivial schemas). Use xsd.exe or a generator like Xsd2Code to produce .NET classes, instantiate the class, then use XmlSerializer to emit valid XML (the serializer will produce the correct element names / xsi:type information). Example pattern:
Dim serializer As New XmlSerializer(GetType(Person))
Dim sb As New StringBuilder()
Using sw As New StringWriter(sb)
    serializer.Serialize(sw, myPersonInstance)
End Using

Dim fragment As New XmlDocument()
fragment.LoadXml(sb.ToString())
rootNode.AppendChild(xmlDoc.ImportNode(fragment.DocumentElement, True))

Extra checks: inspect the XSD for abstract="true" or substitutionGroup declarations, confirm the targetNamespace matches the namespace used when creating nodes, declare the xsi and type prefixes on the root, and validate with a ValidationEventHandler to get precise schema errors. Good call by to use Xsd2Code — generating types and serializing usually avoids the fiddly namespace/xsi issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.