views:

189

answers:

1

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"&gt; 
  <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/"&gt;&gt;
    <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/"&gt;
  <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/")&gt; _
    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/")&gt; _
Public Class DublinCore

    <XmlElement("Property2", Namespace:="http://purl.org/dc/elements/1.1/")&gt; _
    Public Property2 As String

    <XmlElement("Property3", Namespace:="http://purl.org/dc/elements/1.1/")&gt; _
    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

A: 

Although I now think the existing solution is valid for my requirements, there is a way to achieve what I need.

Declare an XmlSerializerNamespaces object within the class to be serialized.

<XmlNamespaceDeclarations()> _
    Public xmlns As XmlSerializerNamespaces

Then add the namespaces to this collection instead of attaching it to the serializer directly. This has the desired effect of declaring the namespace where it is used.

xmlns = New XmlSerializerNamespaces()
        xmlns.Add("oai_dc", "http://www.openarchives.org/OAI/2.0/oai_dc/")
        xmlns.Add("dc", "http://purl.org/dc/elements/1.1/")
        xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance")

Andrew

Andrew Peacock