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. C#
  4. XmlSerializer doesn't write the namespace

XmlSerializer doesn't write the namespace

Scheduled Pinned Locked Moved C#
wcfxmljsontutorialquestion
5 Posts 2 Posters 0 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.
  • B Offline
    B Offline
    buchstaben
    wrote on last edited by
    #1

    Hi there, I'm currently trying to serialize a wsdl.exe generated object

    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myNamespace/")]
    public partial class marketDataEventV1 { //...

    using the following lines of code:

    marketDataEventV1 mdEvent = CreateMarketDataEventV1();
    XmlSerializer ser = new XmlSerializer(typeof(marketDataEventV1));

    StringBuilder sb = new StringBuilder();
    ser.Serialize(new StringWriter(sb), mdEvent);

    Now, having a look to sb.ToString() shows, that the result (string) does not contain the namespace declaration. I wonder now, how to get the serializer taking account for the namespace?! Thanks in advance!

    T 1 Reply Last reply
    0
    • B buchstaben

      Hi there, I'm currently trying to serialize a wsdl.exe generated object

      [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myNamespace/")]
      public partial class marketDataEventV1 { //...

      using the following lines of code:

      marketDataEventV1 mdEvent = CreateMarketDataEventV1();
      XmlSerializer ser = new XmlSerializer(typeof(marketDataEventV1));

      StringBuilder sb = new StringBuilder();
      ser.Serialize(new StringWriter(sb), mdEvent);

      Now, having a look to sb.ToString() shows, that the result (string) does not contain the namespace declaration. I wonder now, how to get the serializer taking account for the namespace?! Thanks in advance!

      T Offline
      T Offline
      Tarakeshwar Reddy
      wrote on last edited by
      #2

      Take a look at this[^] Use the XmlSerializerNamespaces to add your namespace and pass it to the ser.Serialize overload method.


      Tarakeshwar Reddy There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi

      B 1 Reply Last reply
      0
      • T Tarakeshwar Reddy

        Take a look at this[^] Use the XmlSerializerNamespaces to add your namespace and pass it to the ser.Serialize overload method.


        Tarakeshwar Reddy There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi

        B Offline
        B Offline
        buchstaben
        wrote on last edited by
        #3

        Thanks, I've already noticed XmlSerializerNamespaces, but this way, I need to code which namespace belongs to which object. But that's exactly what the [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myNamespace/")]-attribute says. So how to tell the serializer to have a look for these attributes? I expected that as a default..

        T 1 Reply Last reply
        0
        • B buchstaben

          Thanks, I've already noticed XmlSerializerNamespaces, but this way, I need to code which namespace belongs to which object. But that's exactly what the [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myNamespace/")]-attribute says. So how to tell the serializer to have a look for these attributes? I expected that as a default..

          T Offline
          T Offline
          Tarakeshwar Reddy
          wrote on last edited by
          #4

          May be XmlRootAttribute[^] is what you need to use.


          Tarakeshwar Reddy There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi

          B 1 Reply Last reply
          0
          • T Tarakeshwar Reddy

            May be XmlRootAttribute[^] is what you need to use.


            Tarakeshwar Reddy There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi

            B Offline
            B Offline
            buchstaben
            wrote on last edited by
            #5

            That works, thank you. However, I did not expect to need to change the generated code.

            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