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. Sorted xml element while serialising

Sorted xml element while serialising

Scheduled Pinned Locked Moved XML / XSL
questioncsharpxmltutorial
2 Posts 1 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.
  • S Offline
    S Offline
    Soundman32 2
    wrote on last edited by
    #1

    I am using c# serialisation to write some xml. For some reason the output is not necessarily written in the order it appears in the code. How can I order the elements? For example:

    [Serializable]
    public class Request
    {
    [XmlElement("Type")]
    public string Type
    {
    get;
    set;
    }

    [XmlElement("Id")]
    public int Id
    {
    get;
    set;
    }
    }

    Would write

    <Request>
    <Id>1</Id>
    <Type>Get</Type>
    </Request>

    (Note this example is very simplified and doesn't actually write it wrongly).

    S 1 Reply Last reply
    0
    • S Soundman32 2

      I am using c# serialisation to write some xml. For some reason the output is not necessarily written in the order it appears in the code. How can I order the elements? For example:

      [Serializable]
      public class Request
      {
      [XmlElement("Type")]
      public string Type
      {
      get;
      set;
      }

      [XmlElement("Id")]
      public int Id
      {
      get;
      set;
      }
      }

      Would write

      <Request>
      <Id>1</Id>
      <Type>Get</Type>
      </Request>

      (Note this example is very simplified and doesn't actually write it wrongly).

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

      Fixed it. It turns out, fields are serialised before properties and my code was actually like this:

      [Serializable]
      public class Request
      {
      [XmlElement("Type")]
      public string Type
      {
      get;
      set;
      }

      [XmlElement("Id")]
      public int Id; // <<<--- Note this is a field, not a property
      }

      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