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 · Aug 28, 2015 at 07:40 AM · javascriptxmlnamespacenode

Declaring XML Namespace at Runtime

Hi Unity Community!

I’m trying to write a script to add nodes to an XML document at runtime however I am encountering issues when trying to define the name attribute of the new node; I would like to know how I can declare another namespace outside of the default XMLNS attribution (in my case 'name') to match the existing markup.

I assume this is to do with the XmlNameSpaceManager but I am struggling to understand how to implement this (still pretty new to XML in Unity / uJS).

Many thanks in advance,

Ryan

My code in its current state is:

 import System.Runtime.Serialization.Formatters.Binary;
 import System.IO;
 import System.Xml;
 
 public static var control : ObjInfoControl; 
 public var doc : XmlDocument; 
 public var root : XmlNode;
 public var nodeList : XmlNodeList;
 public var docNameSpace : XmlNamespaceManager;
 
 public var health : float; //test
 public var experience : float; //test
 public var objName : String; //imported object name
 public var fileName : String; //original file name
 public var objDescript : String; //user defined description of object
 public var initialObjName : String; //initial name of object chached on load
 
 
 
 function Start(){
     //Automatically loads XML doc for save etc
     doc = new XmlDocument();
     doc.Load(Application.dataPath + "/objMetaTest01.xml");
     root = doc.DocumentElement;    
     nodeList = root.SelectNodes("MetaPipeObject");
     
     for (var i = 0; i < nodeList.Count; i++)
     {
         var node = nodeList[i];    
     }
 }
 
 
 public function CreateNewNode(){
     //Creates new node for new objects
     //will be similar to the replace function to be written soon
 
     //Namespace Manager to add namespace to file
     //docNameSpace = new XmlNamespaceManager(doc.NameTable);
     //docNameSpace.AddNamespace("name", doc.DocumentElement.NamespaceURI);
     //select last MP node to add the new one behind    
     var lastObjNode = root.SelectSingleNode("MetaPipeObject[last()]");
     var newObjNode = doc.CreateElement("MetaPipeObject", "DogTest");
     newObjNode.InnerXml = lastObjNode.InnerXml; //copy content
     root.InsertAfter(newObjNode, lastObjNode); //add to the bottom of the xml doc
     doc.Save(Application.dataPath + "/objMetaTest01.xml");
 }

The return I am currently producing:

   <MetaPipeObject xmlns="DogTest">
     <FileName xmlns="">water_drop.obj</FileName>
     <Description xmlns="">Text to go here</Description>
     <Health xmlns="">10</Health>
     <Experience xmlns="">10</Experience>
   </MetaPipeObject>

I also tested the three string CreateElement() method with better but still not ideal results:

  <MetaPipeObject:name xmlns:MetaPipeObject="DogTest">
     <FileName>water_drop.obj</FileName>
     <Description>Text to go here</Description>
     <Health>10</Health>
     <Experience>10</Experience>
   </MetaPipeObject:name>



The result I am wanting:

   <MetaPipeObject name="DogTest">
     <FileName>water_drop.obj</FileName>
     <Description>Text to go here</Description>
     <Health>10</Health>
     <Experience>10</Experience>
   </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

1 Reply

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

Answer by RyanAchtenSoma · Sep 02, 2015 at 03:30 AM

After much further research it turns out the solution to creating the desired XML format was two part and much simpler than the route I initially researched.

  1. Firstly I was creating the the element using the wrong method; initially I was using the CreateElement(string, string) which was, presumably, invoking the default namespace URI (hence all of the xmlns attributes being associated to each of the child tags). The method I really wanted was the CreateElement(string) which simply provides the name of the element.

  2. All that I needed to do was assign the name attribute via SetAttribute() and all was sorted.

I hope this helps anyone who finds themselves in a similar situation. The NamspaceManager is another possible route I could have used and assigned a different namespace other than the default, however to resolve my issue this seemed overkill.

Here is the snippet of code I ended up using:

 public function CreateNewNode(){
     //Creates new node for new objects
     //will be similar to the replace function to be written soon
 
     var lastObjNode = root.SelectSingleNode("MetaPipeObject[last()]"); //select last MP node to add the new one behind
     
     var newObjNode = doc.CreateElement("MetaPipeObject");
     
     //Create new attribute test
     newObjNode.SetAttribute("name", objName);
 
     newObjNode.InnerXml = lastObjNode.InnerXml; //copy content from above listing
     
     newObjNode.SelectSingleNode("FileName").InnerText = fileName;
     newObjNode.SelectSingleNode("Description").InnerText = "No Description Added Yet";
     newObjNode.SelectSingleNode("Health").InnerText = "0";
     newObjNode.SelectSingleNode("Experience").InnerText = "0";
     
     
     root.InsertAfter(newObjNode, lastObjNode); //add to the bottom of the xml doc
     
     doc.Save(Application.dataPath + "/objMetaTest01.xml");
     
     Debug.Log("CREATED new node: " + newObjNode.GetAttribute("name"));
 
 }

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

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

30 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 avatar image

Related Questions

A node in a childnode? 1 Answer

XmlException: unexpected end of file. Current depth 1 Answer

How can I find specific parts of an XML and feed them into dialogue? 2 Answers

Collectables across multiple levels 3 Answers

Storing XML node and accessing from a dictionary 0 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