By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

Determining if an Attribute is Allowed by the Schema

P: n/a
I'm using an XmlValidatingReader to read an XML document and parse it against
my schema. While I'm reading each node, I need to determine if the schema
allows an ID attribute for the node. The ID is optional so it may not exist
in the XML document. Is there a way to determine if the attribute is allowed
or not? I can't seem to find any information on this anywhere.
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi J

You use the classes in the System.Xml.Schema namespace to do this. Here's
some (inefficient!) code that shows how to read through a document, and test
whether the element can have an attribute called "id"...

HTH

Nigel Armstrong

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim t As New System.Xml.XmlTextReader("C:\testa.xml")
Dim v As New System.Xml.XmlValidatingReader(t)
v.ValidationType = Xml.ValidationType.Schema
AddHandler v.ValidationEventHandler, AddressOf VEH
Dim som As System.Xml.Schema.XmlSchema =
System.Xml.Schema.XmlSchema.Read(New IO.FileStream("C:\test.xsd",
IO.FileMode.Open), AddressOf VEH)
som.Compile(AddressOf VEH)

While v.Read
If v.NodeType = Xml.XmlNodeType.Element Then
Dim oc As System.Xml.Schema.XmlSchemaObjectCollection =
som.Items
For Each o As Object In oc
If TypeOf o Is System.Xml.Schema.XmlSchemaElement Then
If o.Name = v.Name Then
Dim ct As System.Xml.Schema.XmlSchemaComplexType
= o.SchemaType
For Each a As
System.Xml.Schema.XmlSchemaAttribute In ct.Attributes
If a.Name = "id" Then
MessageBox.Show(v.Name & " has an id
attribute")
End If
Next
End If
End If
Next
End If
End While
v.Close()
End Sub

Private Sub VEH(ByVal sender As Object, ByVal e As
System.Xml.Schema.ValidationEventArgs)
Debug.WriteLine(e.Message)
End Sub

"JMonnin" wrote:
I'm using an XmlValidatingReader to read an XML document and parse it against
my schema. While I'm reading each node, I need to determine if the schema
allows an ID attribute for the node. The ID is optional so it may not exist
in the XML document. Is there a way to determine if the attribute is allowed
or not? I can't seem to find any information on this anywhere.

Nov 12 '05 #2

P: n/a
Let's try that again...

This is a little better - but it does rely on the fact that all the elements
in my particular schema are complex types...

HTH more!

Nigel Armstrong

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim t As New System.Xml.XmlTextReader("C:\testa.xml")
Dim v As New System.Xml.XmlValidatingReader(t)
v.ValidationType = Xml.ValidationType.Schema
AddHandler v.ValidationEventHandler, AddressOf VEH

While v.Read
If v.NodeType = Xml.XmlNodeType.Element Then
Dim ct As System.Xml.Schema.XmlSchemaComplexType =
CType(v.SchemaType, System.Xml.Schema.XmlSchemaComplexType)
For Each a As System.Xml.Schema.XmlSchemaAttribute In
ct.Attributes
If a.Name = "id" Then
MessageBox.Show(v.Name & " has an id attribute")
End If
Next
End If
End While
v.Close()
End Sub

Private Sub VEH(ByVal sender As Object, ByVal e As
System.Xml.Schema.ValidationEventArgs)
Debug.WriteLine(e.Message)
End Sub

"JMonnin" wrote:
I'm using an XmlValidatingReader to read an XML document and parse it against
my schema. While I'm reading each node, I need to determine if the schema
allows an ID attribute for the node. The ID is optional so it may not exist
in the XML document. Is there a way to determine if the attribute is allowed
or not? I can't seem to find any information on this anywhere.

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.