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. How to convert XML to text file

How to convert XML to text file

Scheduled Pinned Locked Moved C#
helpcsharpdesignregexxml
5 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.
  • U Offline
    U Offline
    User 12082588
    wrote on last edited by
    #1

    Hello all, I am working on on functionality in which i want to write a c# function which will read XML file from folder. And it will write .txt file from it and will put it in other folder. can some one help me to do this, when i am reading XML file line by line i should able to write any error or log in log file, if some thing is wrong it should rollback all file else generate .txt file. Needs to implement it using any design pattern.

    R T J OriginalGriffO 4 Replies Last reply
    0
    • U User 12082588

      Hello all, I am working on on functionality in which i want to write a c# function which will read XML file from folder. And it will write .txt file from it and will put it in other folder. can some one help me to do this, when i am reading XML file line by line i should able to write any error or log in log file, if some thing is wrong it should rollback all file else generate .txt file. Needs to implement it using any design pattern.

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Technically, an XML file is already a text file. All you need to do is change the file extension. Now, if you actually want to transform the XML in some way, then you're going to need to either write code to do it, or write an XSLT transform[^] to do it. But since you've provided precisely zero details about the input and expected output, nobody can possibly help you.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      1 Reply Last reply
      0
      • U User 12082588

        Hello all, I am working on on functionality in which i want to write a c# function which will read XML file from folder. And it will write .txt file from it and will put it in other folder. can some one help me to do this, when i am reading XML file line by line i should able to write any error or log in log file, if some thing is wrong it should rollback all file else generate .txt file. Needs to implement it using any design pattern.

        T Offline
        T Offline
        trønderen
        wrote on last edited by
        #3

        There are several tools for reading an arbitrary XML file into a DOM tree. Use that, rather than reading XML line by line. This is a plain tree that can easily be traversed like any other tree, usually with a recursive procedure. Depending on the data in the tree and your requirements for the result, you do the traversal depth first or breath first. When you find some value that appears to be text of the kind you are after, you write that to your text output file and continue the search. Maybe there XML structure contains non-text information that you should nevertheless interpret, and produce the appropriate output. E.g. if a node is flagged as a h1, you will probably want to add an extra blank line above the heading text.

        Religious freedom is the freedom to say that two plus two make five.

        1 Reply Last reply
        0
        • U User 12082588

          Hello all, I am working on on functionality in which i want to write a c# function which will read XML file from folder. And it will write .txt file from it and will put it in other folder. can some one help me to do this, when i am reading XML file line by line i should able to write any error or log in log file, if some thing is wrong it should rollback all file else generate .txt file. Needs to implement it using any design pattern.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Member 12116052 wrote:

          XML ... put it in other folder.

          1. Read and process the XML 2. Determine the output. 3. Open the output file 4. Write 2 to the file from 3.

          Member 12116052 wrote:

          when i am reading XML file line by line i s

          Nope. Bad idea. You do not read XML 'line by line'. Rather you parse it. And then extract values from what you parsed. If and ONLY if the source of the XML can be guaranteed to be an automated single source, then you can roll the dice and attempt to write your own parser (not an XML parser) which can parse individual lines. Thus skipping the XML learning curve. And also making your code sure to break if the format of the XML changes.

          1 Reply Last reply
          0
          • U User 12082588

            Hello all, I am working on on functionality in which i want to write a c# function which will read XML file from folder. And it will write .txt file from it and will put it in other folder. can some one help me to do this, when i am reading XML file line by line i should able to write any error or log in log file, if some thing is wrong it should rollback all file else generate .txt file. Needs to implement it using any design pattern.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            To add to what the others say, you need to sit down and work out exactly what you expect to generate form your XML data. The reason being that XML is a text-based hierarchically structured data format, and it can be all in a single line: newlines (or any other whitespace) are not required by the XML specification, and "individual items" in XML can span multiple lines of text without any change in the data it encapsulates or an entire array of objects can occupy a single line of text - that's problem number one! The second problem is that the data contained in the XML itself doesn't "lend itself" to a flat file output - which is what an unformatted text file contains. If you look at a "basic XML" file like this example: W3Schools CD Catalog[^] you can see that what it contains isn't readily useful as a text file unless you actually convert that to a formatted file of some form. Just stripp[ing out the XML stuff doesN't give you any useful data:

            Empire BurlesqueBob DylanUSAColumbia10.901985Hide your heartBonnie TylerUKCBS
            Records9.901988Greatest HitsDolly PartonUSARCA9.901982Still got the bluesGary
            MooreUKVirgin records10.201990ErosEros RamazzottiEUBMG9.901997One night onlyBee
            GeesUKPolydor10.901998Sylvias MotherDr.HookUKCBS8.101973Maggie MayRod
            StewartUKPickwick8.501990RomanzaAndrea BocelliEUPolydor10.801996When a man loves a
            womanPercy SledgeUSAAtlantic8.701987Black angelSavage RoseEUMega10.9019951999 Grammy
            NomineesManyUSAGrammy10.201999For the good timesKenny RogersUKMucik Master8.701995Big
            Willie styleWill SmithUSAColumbia9.901997Tupelo HoneyVan
            MorrisonUKPolydor8.201971SoulsvilleJorn HoelNorwayWEA7.901996The very best
            ofCatStevensUKIsland8.901990StopSam BrownUKA and M8.901988Bridge of
            SpiesT'PauUKSiren7.901987Private DancerTina TurnerUKCapitol8.901983Midt om nattenKim
            LarsenEUMedley7.801983Pavarotti Gala ConcertLuciano PavarottiUKDECCA9.901991The dock of
            the bayOtis ReddingUSAStax Records7.901968Picture bookSimply RedEUElektra7.201985RedThe
            CommunardsUKLondon7.801987Unchain my heartJoe CockerUSAEMI8.201987

            (I manually added the line breaks to prevent that showing as a single very long line!) So you need to work out what is in the XML that you are interested in, and how you will "present" that in your resulting file - otherwise it's just a waste of your time and effort!

            "I have no idea what I did, but

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            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