I have a XML Example:
<Fruits>
<Red_fruits>
<Red_fruits></Red_fruits>
</Red_fruits>
<Yellow_fruits>
<banana></banana>
</Yellow_fruits>
<Red_fruits>
<Red_fruits></Red_fruits>
</Red_fruits>
</Fruits>
I have 4 Red_fruits tags, 2 of them shares the same ParentNode (Fruits), I want to get those which have the same ParentNode.
But I just want those which have the same name (Red_fruits), which means Yellow_fruits tag isn't included.
This is the way I am doing right now using C# language:
XmlDocument doc = new XmlDocument();
string selectedTag = cmbX.text;
if (File.Exists(txtFile.text))
{
try
{
//Load
doc.Load(cmbFile.text);
//Select Nodes
XmlNodeList selectedNodeList = doc.SelectNodes(".//" + selectedTag);
}
Catch
{
MessageBox.show("Some error message here");
}
}
This is returning me all red_fruits, not just the ones that belongs to Fruits.
I can't make XmlNodeList = doc.SelectNodes("/Fruits/Red_fruits") because I want to use this code to read random XML files, so I don't know the exact name that specific node will have, I just need to put all nodes with the same name and same level into a XmlNodeList using C# Language.
Is there a way of achieve this without using LINQ? How to do that?
Descendants("Red_Fruits")
would return anything that has "Red_Fruits" tag. :) – KierkegaardCabbage
elements from this XML:<Foo><Rincewind><Cabbage>...</Cabbage></Rincewind><Womble><Cabbage>...</Cabbage></Womble></Foo>
? Will it be both cabbages, or grouped based on what their parent element is? Basically I think your question is woefully underspecified. – Papule"same ParentNode"
. Same parent node and same depth are 2 different things. – PrincipledRed_fruits
at 2 different depths. Let's call these depths level 2 and level 3. Which of these depths do you want to get the red fruits from? and what makes that depth correct and the other depth wrong? – Principleddoc.SelectNodes("//" + parentTag + "/" + selectedTag)
? – Principled