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. XML lookup table [modified]

XML lookup table [modified]

Scheduled Pinned Locked Moved C#
xmlquestion
3 Posts 2 Posters 1 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
    mail572352
    wrote on last edited by
    #1

    Hi, I want to store a set of codes in an XML file and read the contents. I want each object to be a item consisting of a name and a code, so that I can put the name in and get the corresponding code out. What's a simple and efficient way of doing this? I can't use my web.config as there may be many codes. Thanks :)

    B 1 Reply Last reply
    0
    • M mail572352

      Hi, I want to store a set of codes in an XML file and read the contents. I want each object to be a item consisting of a name and a code, so that I can put the name in and get the corresponding code out. What's a simple and efficient way of doing this? I can't use my web.config as there may be many codes. Thanks :)

      B Offline
      B Offline
      Brady Kelly
      wrote on last edited by
      #2

      Assuming your object value can be rendered as a string, XmlTextWriter and XPath are your simplest tools. A simple WriteElementString to write each object with the element name for your object name, and the element value your object value. Then for reading you load the file into an XmlDocument, and use an XPath query for the element with your object name, and get its value. public static void Write() { using (XmlTextWriter tw = new XmlTextWriter(myFileName, System.Text.Encoding.Default)) { tw.WriteStartDocument(); tw.WriteElementString("ObjectName", "ObjectValue"); tw.WriteEndDocument(); } } public static void Read() { XmlDocument xdoc = new XmlDocument(); xdoc.Load(myFileName); XmlNode myObject = xdoc.SelectSingleNode("ObjectName"); string myValue = ""; if (myObject != null) myValue = myObject.InnerText; }

      M 1 Reply Last reply
      0
      • B Brady Kelly

        Assuming your object value can be rendered as a string, XmlTextWriter and XPath are your simplest tools. A simple WriteElementString to write each object with the element name for your object name, and the element value your object value. Then for reading you load the file into an XmlDocument, and use an XPath query for the element with your object name, and get its value. public static void Write() { using (XmlTextWriter tw = new XmlTextWriter(myFileName, System.Text.Encoding.Default)) { tw.WriteStartDocument(); tw.WriteElementString("ObjectName", "ObjectValue"); tw.WriteEndDocument(); } } public static void Read() { XmlDocument xdoc = new XmlDocument(); xdoc.Load(myFileName); XmlNode myObject = xdoc.SelectSingleNode("ObjectName"); string myValue = ""; if (myObject != null) myValue = myObject.InnerText; }

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

        That's great, thanks :)

        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