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 do I find a XML element based on attribute

How do I find a XML element based on attribute

Scheduled Pinned Locked Moved C#
questiondatabasexmlhelptutorial
4 Posts 3 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.
  • O Offline
    O Offline
    Ola E
    wrote on last edited by
    #1

    I'm new to XML, but wat I'm trying to do is this. I have a XML file with config settings and language items e.g. com1 9600 I read the Config elements by using GetElementByTagName, so this is no problem. I'm looking for a simmular way of selecting the language items so I can do somthing like psedu code Lang = XMLSetup.GetElementsByTagName("Language")[0]; XmlElement LangText= (XmlElement)Setup; textbox.text=LangText.GetElementByAttribute("No","1000").GetAttribute("Text") The last line selects the "value on an attribute, depending on an other attribute" Could this be done? If I change the structure of of my XML file to somthing like... ... Hello world could it be done then?? Any ideas on how to format the XML and do the selection? I'm used to work with databases, and XML could be used as a databse, or??? If you use an SQL table, you can simply do "select Text from Language where No="1000"... Thanksfull for any advice:confused:

    P 1 Reply Last reply
    0
    • O Ola E

      I'm new to XML, but wat I'm trying to do is this. I have a XML file with config settings and language items e.g. com1 9600 I read the Config elements by using GetElementByTagName, so this is no problem. I'm looking for a simmular way of selecting the language items so I can do somthing like psedu code Lang = XMLSetup.GetElementsByTagName("Language")[0]; XmlElement LangText= (XmlElement)Setup; textbox.text=LangText.GetElementByAttribute("No","1000").GetAttribute("Text") The last line selects the "value on an attribute, depending on an other attribute" Could this be done? If I change the structure of of my XML file to somthing like... ... Hello world could it be done then?? Any ideas on how to format the XML and do the selection? I'm used to work with databases, and XML could be used as a databse, or??? If you use an SQL table, you can simply do "select Text from Language where No="1000"... Thanksfull for any advice:confused:

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The trick to doing this is fairly easy://Item[@No='1000']Basically, this searches for all elements from the root that match Item where there is an attribute No with the value 1000. The @ symbol is used to indicate that it's an attribute.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles | MoXAML PowerToys

      O 1 Reply Last reply
      0
      • P Pete OHanlon

        The trick to doing this is fairly easy://Item[@No='1000']Basically, this searches for all elements from the root that match Item where there is an attribute No with the value 1000. The @ symbol is used to indicate that it's an attribute.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        O Offline
        O Offline
        Ola E
        wrote on last edited by
        #3

        I don't follow you here... Can you give me an example of how to write that in C#? Thanks!:confused:

        T 1 Reply Last reply
        0
        • O Ola E

          I don't follow you here... Can you give me an example of how to write that in C#? Thanks!:confused:

          T Offline
          T Offline
          Tuwing Sabado
          wrote on last edited by
          #4
                  System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
                  xmlDocument.Load(@"c:\\text.xml");
          
          
                  // Fetch all 'item' xml elements with attribute 'No' equal to 1000 
                  System.Xml.XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//Item\[@No='1000'\]");
          
                  foreach (System.Xml.XmlNode xmlNode in xmlNodeList)
                  {
                      // Display the value of 'Item' xml element
                      Console.WriteLine(xmlNode.Value); 
                  }
          

          happy coding :) regards, mark

          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