.net XMLSerializer - multiple namespace problem
-
Hi, Apologies if this in the wrong place but its the first time I've posted. I've got a problem using the .net XMLSerializer object. My experience of this is limited and I haven't worked with XML much. 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:Property2asdsd</dc:Property2>
dc:Property3asdasasdda</dc:Property3>
</oai_dc:dc>
</Record>however the best I can get at the moment is
<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:Property2asdsd</dc:Property2>
dc:Property3asdasasdda</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'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
-
Hi, Apologies if this in the wrong place but its the first time I've posted. I've got a problem using the .net XMLSerializer object. My experience of this is limited and I haven't worked with XML much. 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:Property2asdsd</dc:Property2>
dc:Property3asdasasdda</dc:Property3>
</oai_dc:dc>
</Record>however the best I can get at the moment is
<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:Property2asdsd</dc:Property2>
dc:Property3asdasasdda</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'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
Hi, this might not the answer you expected, but what is wrong with the namespaces declared within the root element? Both xml are correct, and express the same. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
Hi, this might not the answer you expected, but what is wrong with the namespaces declared within the root element? Both xml are correct, and express the same. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
Thanks for that, my limited knowledge of XML is the problem here. I have found a way to render the XML as I had intended though. This might be useful to someone else at some point... Declare an XmlSerializerNamespaces object within the class to be serialized.
<XmlNamespaceDeclarations()> _
Public xmlns As XmlSerializerNamespacesThen 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