- Home /
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[i];
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:
</MetaPipeObject>
<MetaPipeObject name="Logs with graffiti">
<FileName>TestObj_EvanLogs04.obj</FileName>
<MeshLocation>/Users/ryanachten/Documents/UnityTests/MetaPipeline_TestModels/CRM_TestObjs/TestObj_EvanLogs/TestObj_EvanLogs04/TestObj_EvanLogs04.obj</MeshLocation>
<TexLocation>/Users/ryanachten/Documents/UnityTests/MetaPipeline_TestModels/CRM_TestObjs/TestObj_EvanLogs/TestObj_EvanLogs03/TestObj_EvanLogs.jpg</TexLocation>
<ObjImgCap>/Users/ryanachten/Documents/UnityTests/MetaPipeline/Assets/ScreenShot.png</ObjImgCap>
<ContribName>Ryan Achten</ContribName>
<ContribDate>03/10/2015</ContribDate>
<Description>Logs with graffiti found out by Evan's Bay</Description>
<ModelInfo>
<ModelCreator>Ryan Achten</ModelCreator>
<ModelCreateDate>31/05/2015</ModelCreateDate>
<ModelCreateType>Photogrammetric</ModelCreateType>
</ModelInfo>
<ContextualInfo />
</MetaPipeObject>
Answer by RyanAchtenSoma · Oct 06, 2015 at 05:28 AM
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.
Answer by cjdev · Oct 05, 2015 at 09:40 AM
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;
}
Hey @cjdev, thanks for your response. $$anonymous$$uch too invested in the XmlDocument approach to turn back on it at this stage. However would you $$anonymous$$d explaining the logic behind your suggestion? I may be able to apply this within the XmlDoc class. Thanks in advance!
Basically it just searches through all of the nodes for any that contain a value with 'Logs' in it, grabs the first one it sees, and gets the 'name' attribute's value from the parent.
Yeah that's exactly what I was trying to achieve in my code above, thanks anyway
Your answer
Follow this Question
Related Questions
how do I search for an atribute in a XML file? 0 Answers
Add letters to string without 26 if-statements? 1 Answer
Converting String Array to Int. 2 Answers
A node in a childnode? 1 Answer
Equivalent to PHP strstr() in Unity to find a string? 2 Answers