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. XmlTextReader - reading from a string?

XmlTextReader - reading from a string?

Scheduled Pinned Locked Moved C#
xmlperformancequestion
2 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.
  • A Offline
    A Offline
    andrewpritchie
    wrote on last edited by
    #1

    All, Just wondering if it is possible to parse XML from a string in memory, as opposed to a file? I'm getting XML data over a TCP/IP connection, and it is read into a string in memory. I could dump the string to a file, then parse the filename to XmlTextReader, but that seems like a waste of resources. Any pointers would be appreciated. Cheers, Andrew

    L 1 Reply Last reply
    0
    • A andrewpritchie

      All, Just wondering if it is possible to parse XML from a string in memory, as opposed to a file? I'm getting XML data over a TCP/IP connection, and it is read into a string in memory. I could dump the string to a file, then parse the filename to XmlTextReader, but that seems like a waste of resources. Any pointers would be appreciated. Cheers, Andrew

      L Offline
      L Offline
      Lars Niedziolka
      wrote on last edited by
      #2

      Hi Andrew, there are some solutions: 1st) If you only want an XmlDocument, use the methode LoadXml of the XmlDocument class. string myXmlContent = @"<GoodSides><Side name='CodeProject'>http://www.codeproject.com</Side></GoodSides>"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(myXmlContent); 2nd) Copy the bytes (or strings) to a MemoryStream. byte[] myContent = ...; MemoryStream memStream = new MemoryStream(myContent); XmlTextReader xmlReader = new XmlTextReader(memStream); or MemoryStream memStream = new MemoryStream(); StreamWriter memWriter = new StreamWriter(memStream); memWriter.Write(@"<GoodSides>"); memWriter.Write(@"<Side name='CodeProject'>http://www.codeproject.com</Side>"); memWriter.Write(@"</GoodSides>"); memStream.Position = 0; // Reset the position XmlTextReader xmlReader = new XmlTextReader(memStream); 3rd) Implement a own class with Stream as BaseClass. This class can read the datas direct from TCP/IP. class MyOwnStream: Stream { // especialy int Read(byte[] buffer, int offset, int count) { ... } }; MyOwnStream myStream = new MyOwnStream(...); XmlTextReader xmlReader = new XmlTextReader(myStream); 4th) Use the SocketStream direct. (If the stream contains only the xml data and no more.) NetworkStream myNetworkStream = new NetworkStream(mySocket); XmlTextReader xmlReader = new XmlTextReader(myStream); Hope, it helps Niedzi

      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