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 Kalu · Sep 01, 2011 at 10:21 AM · xmlread

XML Check if a node exists

I have set up an XML in which I will write data. Each node is unique, like this:

 <transforms>
 
 <Exterieur>
 
      <MurExterieur> BriqueViolette </MurExterieur>
 </Exterieur>
 
    <Interieur>
 
      <MurInterieur> Texture </MurInterieur>
    </Interieur>
 
 </transforms>

I can not do:

-When I edit the XML file, I must see if the node is already exists,

  • If there is, I do not create the node, but I write in,

  • If there is, I create and write in.

My problem is that I can not retrieve the node. I try with this method:

 XmlElement = elmExistorNot xmlDoc.GetElementById (rootNodeName);

=> I get reference "null"

and the second uses an array of Nodes, or I'm sure each of my nodes will be unique ...

Need help :]

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

3 Replies

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

Answer by CHPedersen · Sep 01, 2011 at 10:46 AM

Your approach is actually not too far off the mark, but you're using the wrong getelement method. GetElementById searches for an attribute called ID, and then attempts to match the argument up against it. It returns null for you, because your XML doesn't have any nodes with an ID attribute with a name corresponding to the rootNodeName.

Instead, use XmlDocument.GetElementsByTagName, see this link:

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.getelementsbytagname.aspx

It returns a list of all elements whose tagname is what you provide, and since you mentioned that all your nodes have unique tag names, the returned array will always have length = 1 if the element already exists, and the node you want will always be at index = 0 if it exists. If the returned array has length = 0, it's because the node doesn't exist and you can go ahead and create it.

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 thor_tillas · Sep 01, 2011 at 11:02 AM

Hi,

As I see it, you have to do something like that :

 public void ReadXmlSettings()
 {
     System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
     xmlDocument.Load("myXmlSettings.xml");

     // Get the root element (which is "transforms" in your case).
     System.Xml.XmlElement root = xmlDocument.DocumentElement;

     // Get the Exterieur element or create it when does not exist.
     System.Xml.XmlElement exterieurElement = this.GetChildByName(root, "Exterieur", xmlDocument);

     // Get the Interieur element or create it when does not exist.
     System.Xml.XmlElement interieurElement = this.GetChildByName(exterieurElement, "Interieur", xmlDocument);

     // Do something with interieur element...
 }

 private System.Xml.XmlElement GetChildByName(System.Xml.XmlElement parent, string childName, System.Xml.XmlDocument xmlDocument)
 {
     // Try to find it in the parent element.
     System.Xml.XmlElement childElement = parent.SelectSingleNode(childName) as System.Xml.XmlElement;
     if (null == childElement)
     {
         // The child element does not exists, so create it.
         childElement = xmlDocument.CreateElement(childName);
         parent.AppendChild(childElement);
     }

     return childElement;
 }

Does it correspond to your need ?

Otherwise, you can create a "Settings" class and serialize/deserialize it with the Serializer class. Here is the MSDN documentation about it : http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

It's pretty simple to use and it's work well for simple serialization.

Cheers !

Comment
Add comment · Show 2 · 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 Kalu · Sep 01, 2011 at 11:34 AM 0
Share

Thx guy's i'll try it! :)

avatar image Kalu Kalu · Sep 01, 2011 at 11:47 AM 0
Share

Work perfectly. Thx a lot.

avatar image
0

Answer by thor_tillas · Sep 01, 2011 at 01:00 PM

You have many ways to doing so. But if your goal is to get some "Settings" on the begining of your game, I really suggest you to look after the Serializer stuff. All you need is a Class which describe your settings as "Properties" and add these line where you need to get the settings back from XML.

XmlSerializer serializer = new XmlSerializer(typeof(SettingsClass));
System.IO.FileStream fs = new System.IO.FileStream(xmlPath, System.IO.FileMode.Open);
SettingsClass settings = (SettingsClass)serializer.Deserialize(fs);

If you want to create the xml based on the class :

SettingsClass settings = new SettingsClass();
XmlSerializer serializer = new XmlSerializer(typeof(SettingsClass));
System.IO.FileStream fs = new System.IO.FileStream(xmlPath, System.IO.FileMode.Create);
serializer.Serialize(fs, settings);

I don't know if it's a correct solution for your case, but it seams to me a more developper friendly way to get some settings saved.

I hope it can help...

Cheers !

Comment
Add comment · Show 4 · 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 Kalu · Sep 01, 2011 at 12:17 PM 0
Share

Now I need to read all the xml node after node, for that I must target the nodes one after the other and get their content.

In my view, be there a method to move the nodes one after the other, or I must stock every node name in a table to go with getElementByTagName. Can someone enlighten me :]]?

avatar image Kalu · Sep 01, 2011 at 01:43 PM 0
Share

Thank you very much everyone for all the quick answers. I seek and I find a way to do that work (the goal is to store textures and the object on which it is applied in order to transfer it to another model):

if(File.Exists (filepath))
{
xmlDoc.Load(filepath);

XmlNodeReader xmlReader = new XmlNodeReader(xmlDoc);

while (xmlReader.Read()) {

xmlValue = xmlReader.Value;

if(xmlReader.Name != "") {

xmlName = xmlReader.Name;

} if(xmlReader.HasValue) {

Texture2D txtrLoader = Resources.Load("Texture/"+xmlValue, typeof(Texture2D)) as Texture2D;

Transform sousObject = targetHouse.transform.FindChild(xmlName);

sousObject.renderer.material.mainTexture = txtrLoader; }

}

}

I would like you to tell me if the "serializer" you speak of is more optimize or perform?!

avatar image thor_tillas · Sep 01, 2011 at 02:08 PM 0
Share

No, I think it's clearly less optmized. The serializer need to get the class properties by reflection and then set them some value. But it allows to keep a code more clean in certain case.

I think your solution is well adapted to your needs.

avatar image Kalu · Sep 01, 2011 at 02:28 PM 0
Share

Thx for u reply! And thx all for ur help :]]

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Write and Read to a iOS device! Help 1 Answer

Xml Serialization? (how to read/write?) 1 Answer

A node in a childnode? 1 Answer

Reading XML Data C# 2 Answers

Read STRING AS XML TO SHOW LIST 1 Answer


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