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. Write data to an xml file

Write data to an xml file

Scheduled Pinned Locked Moved C#
csharpxmlquestion
6 Posts 5 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.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!

    N G A 3 Replies Last reply
    0
    • Y Yustme

      Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello In System.Xml namespace there are two classes: XmlReader and XmlWriter. Use them to make the xml file and write the data to it.

      Regards:rose:

      1 Reply Last reply
      0
      • Y Yustme

        Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Use the ToString method of the DateTime class to create the string representation of the date and the time. As you have used the exact format that the ToString method uses, it's easy: daDate.ToString("ddMMyyyy") and daDate.ToString("HH:mm:ss")

        --- b { font-weight: normal; }

        L 1 Reply Last reply
        0
        • G Guffa

          Use the ToString method of the DateTime class to create the string representation of the date and the time. As you have used the exact format that the ToString method uses, it's easy: daDate.ToString("ddMMyyyy") and daDate.ToString("HH:mm:ss")

          --- b { font-weight: normal; }

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          Using daDate.ToString("HH:mm:ss") is a risky choice as ":" is a placeholder for the time separator. Hence for example Italian regional settings would give something like "21.17.48" instead of "21:17:48". Even in a fixed region users could simply break the format by changing their regional settings. Always provide a IFormatProvider, typically CultureInfo.InvarientCulture. If you create your own CultureInfo instance to do the formatting, remember specifying "false" to disallow custom overrides. I assume this is a fixed XML format that MUST be followed? If it is I guess you do not have a choice, but if it is in ANY way possible to influence the format do so, as it is basically as wrong as it can be. When storing dates as strings there are two formats you can use: "yyyy-MM-dd" or wrong. "ddMMyyyy" clearly belongs in the "wrong" category as it is ambigious and it doesn't even sort correctly. When storing date and time, preferably write them into a single entry with the ISO format. If you use the XmlSerializer, just serialize/deserialize a DateTime object and it will use the correct ISO format.

          G 1 Reply Last reply
          0
          • L lmoelleb

            Using daDate.ToString("HH:mm:ss") is a risky choice as ":" is a placeholder for the time separator. Hence for example Italian regional settings would give something like "21.17.48" instead of "21:17:48". Even in a fixed region users could simply break the format by changing their regional settings. Always provide a IFormatProvider, typically CultureInfo.InvarientCulture. If you create your own CultureInfo instance to do the formatting, remember specifying "false" to disallow custom overrides. I assume this is a fixed XML format that MUST be followed? If it is I guess you do not have a choice, but if it is in ANY way possible to influence the format do so, as it is basically as wrong as it can be. When storing dates as strings there are two formats you can use: "yyyy-MM-dd" or wrong. "ddMMyyyy" clearly belongs in the "wrong" category as it is ambigious and it doesn't even sort correctly. When storing date and time, preferably write them into a single entry with the ISO format. If you use the XmlSerializer, just serialize/deserialize a DateTime object and it will use the correct ISO format.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            lmoelleb wrote:

            Using daDate.ToString("HH:mm:ss") is a risky choice as ":" is a placeholder for the time separator.

            Yes, you are right. :) I didn't think of that. The character should be quoted to be interpreted as a literal: "HH':'mm':'ss" As the format now contains no culture variant components, it's not affected by any cultural differences. Using a format provider doesn't affect the output in this case, it's only encouraged for programming style. If no format provider is specified, the CultureInfo.CurrentCulture is used, as there exists no formatting without a format provider.

            --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • Y Yustme

              Hi, How can i create and write to an xml file with C# the date and time in seperate tags to an xml file like this: ddMMyyyy HH:mm:ss Thanks in advance!

              A Offline
              A Offline
              Arjun Mjolnir Bahree
              wrote on last edited by
              #6

              I assume that you already know how to write an XML output. So to segregate the Dat and time, you can use the DateTime class to get the date and time seperate portions from the DateTime.Now Just take out the time and date component from the supported methods

              Excelsior Arjun Bahree "By The Might of Mjolnir" I Came! I Coded! I Conquered!

              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