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. Parsing an XML File

Parsing an XML File

Scheduled Pinned Locked Moved C#
csharplinqxmljsonhelp
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.
  • M Offline
    M Offline
    MarkB123
    wrote on last edited by
    #1

    Hi, Can someone help me parse the following XML with DotNet C# 4.0? I'm trying to do it with linq. XML is as follows... 0 "Success" "(Job 1) No Errors detected" 0 "Success" "(Job 2) Scheduling Complete. All available orders were planned." "" "A24" "" "" "" "" "XXX" "Run-0001" 1 4 0 0.00000 0.00000 "" "" "" "" "" 0.00 0.00 "XXX" "00:00:00" "00:00:00" "00:00:00" "00:00:00" 0 0 0 0 0 "Mon" "19:00:00" "Mon" "19:00:00" 0 0 0.000 0.0000 2 "" "A24" "A24" 61.000 102 102 "PDA3" "761" "YYY" "111" "222" "0.00" "0.00" "XXX" "Run-0001" 2 2 2 0.00000 0.00000 "" "" "" "" <

    Richard DeemingR 1 Reply Last reply
    0
    • M MarkB123

      Hi, Can someone help me parse the following XML with DotNet C# 4.0? I'm trying to do it with linq. XML is as follows... 0 "Success" "(Job 1) No Errors detected" 0 "Success" "(Job 2) Scheduling Complete. All available orders were planned." "" "A24" "" "" "" "" "XXX" "Run-0001" 1 4 0 0.00000 0.00000 "" "" "" "" "" 0.00 0.00 "XXX" "00:00:00" "00:00:00" "00:00:00" "00:00:00" 0 0 0 0 0 "Mon" "19:00:00" "Mon" "19:00:00" 0 0 0.000 0.0000 2 "" "A24" "A24" 61.000 102 102 "PDA3" "761" "YYY" "111" "222" "0.00" "0.00" "XXX" "Run-0001" 2 2 2 0.00000 0.00000 "" "" "" "" <

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

      You're calling .Elements("row") on the XDocument instance. The document only contains a single element, which is the root element of the XML document. You'll need to use the Descendants method to find the rows. You'll also need to restrict the scope to the relevant USEROUT node, since the row elements under the first node don't match your expected layout.

      List jobList = XDocument.Parse(responseString)
      .Descendants("USEROUT")
      .Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
      .Descendants("row")
      .Select(item => new Callout { ... })
      .ToList();


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

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

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You're calling .Elements("row") on the XDocument instance. The document only contains a single element, which is the root element of the XML document. You'll need to use the Descendants method to find the rows. You'll also need to restrict the scope to the relevant USEROUT node, since the row elements under the first node don't match your expected layout.

        List jobList = XDocument.Parse(responseString)
        .Descendants("USEROUT")
        .Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
        .Descendants("row")
        .Select(item => new Callout { ... })
        .ToList();


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

        M Offline
        M Offline
        MarkB123
        wrote on last edited by
        #3

        Richard, Firstly, thanks for taking the time to respond. You answer was exactly what I needed - many thanks for this. One quick question... Is it possible to add some sort of where clause into it so that only those rows with a TASK_TYPE = "2" are returned? Sorry, I'm new to parsing XML... :)

        Richard DeemingR 1 Reply Last reply
        0
        • M MarkB123

          Richard, Firstly, thanks for taking the time to respond. You answer was exactly what I needed - many thanks for this. One quick question... Is it possible to add some sort of where clause into it so that only those rows with a TASK_TYPE = "2" are returned? Sorry, I'm new to parsing XML... :)

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Something like this should do the trick:

          List<Callout> jobList = XDocument.Parse(responseString)
          .Descendants("USEROUT")
          .Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
          .Descendants("row")
          .Where(item => string.Equals((string)item.Attribute("TASK_TYPE"), "2", StringComparison.Ordinal))
          .Select(item => new Callout { ... })
          .ToList();


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

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

          M 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Something like this should do the trick:

            List<Callout> jobList = XDocument.Parse(responseString)
            .Descendants("USEROUT")
            .Where(el => string.Equals((string)el.Attribute("filename"), "User_out.csv", StringComparison.OrdinalIgnoreCase))
            .Descendants("row")
            .Where(item => string.Equals((string)item.Attribute("TASK_TYPE"), "2", StringComparison.Ordinal))
            .Select(item => new Callout { ... })
            .ToList();


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

            M Offline
            M Offline
            MarkB123
            wrote on last edited by
            #5

            Thanks again Richard.

            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