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,

Ryan

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:


    TestObj_EvanLogs04.obj
    /Users/ryanachten/Documents/UnityTests/MetaPipeline_TestModels/CRM_TestObjs/TestObj_EvanLogs/TestObj_EvanLogs04/TestObj_EvanLogs04.obj
    /Users/ryanachten/Documents/UnityTests/MetaPipeline_TestModels/CRM_TestObjs/TestObj_EvanLogs/TestObj_EvanLogs03/TestObj_EvanLogs.jpg
    /Users/ryanachten/Documents/UnityTests/MetaPipeline/Assets/ScreenShot.png
    Ryan Achten
    03/10/2015
    Logs with graffiti found out by Evan’s Bay

    Ryan Achten
    31/05/2015
    Photogrammetric


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"))
                                                .Single().Parent.Attribute("name").Value;
}

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.