Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. .net XMLSerializer - multiple namespace problem

.net XMLSerializer - multiple namespace problem

Scheduled Pinned Locked Moved XML / XSL
csharpxmlhelp
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndyPeacock
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • A AndyPeacock

      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

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • S SeMartens

        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.

        A Offline
        A Offline
        AndyPeacock
        wrote on last edited by
        #3

        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 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups