Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RyanAchtenSoma · Oct 04, 2015 at 11:27 PM · stringxmlsearch

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>
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanAchtenSoma · Oct 05, 2015 at 10:18 AM 0
Share

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!

avatar image cjdev · Oct 05, 2015 at 08:21 PM 1
Share

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.

avatar image RyanAchtenSoma cjdev · Oct 05, 2015 at 08:51 PM 0
Share

Yeah that's exactly what I was trying to achieve in my code above, thanks anyway

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges