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. LINQ
  4. How can we read XML file , line by line ?

How can we read XML file , line by line ?

Scheduled Pinned Locked Moved LINQ
csharplinqxmlhelpquestion
4 Posts 2 Posters 2 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
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hi all. I have a XML file. something like this :

    <FoxMark ELEMENTS="442">
    <BOOKMARK1 ID="1" />
    <BOOKMARK2 ID="2" />
    <NODE4 ID="4" NAME="FireFox" DESC="" />
    <BOOKMARK5 ID="5" />
    <BOOKMARK6 ID="6" />
    <BOOKMARK7 ID="7" />
    <BOOKMARK8 ID="8" />
    <BOOKMARK9 ID="9" />
    <BOOKMARK10 ID="10" />
    <BOOKMARK11 ID="11" />
    <BOOKMARK12 ID="12" />
    <BOOKMARK13 ID="13" />
    </NODE4>
    </FoxMark>

    I want to read this file, line by line and LINQ. Can you help me?

    Freshman

    J 1 Reply Last reply
    0
    • M Mohammad Dayyan

      Hi all. I have a XML file. something like this :

      <FoxMark ELEMENTS="442">
      <BOOKMARK1 ID="1" />
      <BOOKMARK2 ID="2" />
      <NODE4 ID="4" NAME="FireFox" DESC="" />
      <BOOKMARK5 ID="5" />
      <BOOKMARK6 ID="6" />
      <BOOKMARK7 ID="7" />
      <BOOKMARK8 ID="8" />
      <BOOKMARK9 ID="9" />
      <BOOKMARK10 ID="10" />
      <BOOKMARK11 ID="11" />
      <BOOKMARK12 ID="12" />
      <BOOKMARK13 ID="13" />
      </NODE4>
      </FoxMark>

      I want to read this file, line by line and LINQ. Can you help me?

      Freshman

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      If all you want is to investigate the lines, and you don't want to modify the XML or parse individual fields out of it, you can do it like this, no LINQ required.

      var lines = File.ReadAllLines("pathToMyXMLFile.xml");

      If you need to investigate individual fields or change data in the XML, I recommend looking at the XDocument[^] class. This LINQ to XML overview[^] can get you started with writing LINQ queries against XML documents.

      M 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        If all you want is to investigate the lines, and you don't want to modify the XML or parse individual fields out of it, you can do it like this, no LINQ required.

        var lines = File.ReadAllLines("pathToMyXMLFile.xml");

        If you need to investigate individual fields or change data in the XML, I recommend looking at the XDocument[^] class. This LINQ to XML overview[^] can get you started with writing LINQ queries against XML documents.

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        Thank you very much Judah . But How we can convert XML's characters (like these: ) to the original characters.

        J 1 Reply Last reply
        0
        • M Mohammad Dayyan

          Thank you very much Judah . But How we can convert XML's characters (like these: ) to the original characters.

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Have a look at SecurityElement.Escape[^] method. Since you want to be working with the decoded XML special characters in the values of the XML, you might as well just use an XDoc. Here's how you can get elements in the XML by string, with the decoded special characters:

          var doc = XDocument.Parse( YOUR XML GOES HERE );
          var decodedElements = from element in doc.Elements()
                                select element.ToString();

          Alternately, and I don't know if this covers all your bases, you could look at System.Web.HttpUtility.HtmlDecode.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          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