|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Determining if an Attribute is Allowed by the SchemaI'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. 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 Show quote "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. 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 Show quote "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. |
|||||||||||||||||||||||