Searching XML document

Hi Unity Community

I am trying to search through an XML document (using the XmlDocument class) nodes containing a specified string (in this test I’’m looking for 'Logs’) and then catch the value of matching node parent’s name attribute.

However I’m messing this up somewhere along the way, it’s currently returning each of the node parent names in the doc (as opposed to just the names of those who match). Can anyone help with my logic to make this work properly?

Many thanks in advance,


Current code:

	var mpo = xmlRoot.SelectNodes("MetaPipeObject");
	for (var i = 0; i < mpo.Count; i++)
		var curNode = mpo*;*

_ var nodeCheck = curNode.SelectNodes(“//*[contains(text(), ‘Logs’)]”);_

  •  Debug.Log("nodeCheck: " + nodeCheck.Count);*
  •  if (nodeCheck.Count > 1)*
  •  {*
  •  	Debug.Log(curNode.SelectSingleNode("@name").Value);*
  •  }*
  • }*
    Example XML snippet:

    Ryan Achten
    Logs with graffiti found out by Evan’s Bay

    Ryan Achten

I’m not quite sure what it would be using the XmlDocument class but if you don’t mind switching over to Linq and using the XDocument and XElement classes then you could do something like this:

void Start()
    TextAsset xmlText = Resources.Load("xmlDoc") as TextAsset;
    XDocument doc = XDocument.Parse(xmlText.text);
    string parentName = doc.Root.Descendants().Where(x => x.Value.Contains("Logs"))

Solved. The issue was that I was not selecting the current node in the xpath expression before searching the contents of its children. This should have been:

var nodeCheck = curNode.SelectNodes(".//*[contains(text(), 'Logs')]");

Notice the ‘.’ before the ‘//’ was missing in the previous xpath expression. Hopefully this helps out anyone else in a similar situation.