How do I find a XML element based on attribute
-
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:
-
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:
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.
-
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.
-
I don't follow you here... Can you give me an example of how to write that in C#? Thanks!:confused:
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