Hi,
I've got a problem using the .net XMLSerializer object and XML namespaces. My experience of the the XMLSerializer is very limited and I haven't worked with XML much before. I'm trying to something like the following:
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Property1>blah</Property1>
<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">>
<dc:Property2>asdsd</dc:Property2>
<dc:Property3>asdasasdda</dc:Property3>
</oai_dc:dc>
</Record>
however the best I can get at the moment is (note the position of the namespace declarations)
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<Property1>blah</Property1>
<oai_dc:dc>
<dc:Property2>asdsd</dc:Property2>
<dc:Property3>asdasasdda</dc:Property3>
</oai_dc:dc>
</Record>
The namespaces are being declared in the root element, not on the dc element as I would like. I understand from that either declaring the namespace in the root or the element that uses the namespace should be acceptable.
I'm attaching the XMLSerializerNamespaces as follows:
Dim XMLNamespaces As New XmlSerializerNamespaces()
XMLNamespaces.Add("oai_dc", "http://www.openarchives.org/OAI/2.0/oai_dc/")
XMLNamespaces.Add("dc", "http://purl.org/dc/elements/1.1/")
XMLNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance")
XMLSerializer.Serialize(XMLWriter, inputObject, XMLNamespaces)
and the objects I'm using are shown below:
<XmlRoot("Record")> _
Public Class Record
<XmlElement("Property1")> _
Public Property1 As String
<XmlElement("dc", Namespace:="http://www.openarchives.org/OAI/2.0/oai_dc/")> _
Public DCMetadata As DublinCore
Public Sub New()
DCMetadata = New DublinCore()
End Sub
End Class
<XmlRoot("dc", Namespace:="http://www.openarchives.org/OAI/2.0/oai_dc/")> _
Public Class DublinCore
<XmlElement("Property2", Namespace:="http://purl.org/dc/elements/1.1/")> _
Public Property2 As String
<XmlElement("Property3", Namespace:="http://purl.org/dc/elements/1.1/")> _
Public Property3 As String
Public Sub New()
End Sub
End Class
Is there any way to force the XMLSerializer to output the namespace declarations against an element further down the tree?
Thanks for any input,
Andy