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*; *
- }*
}
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:
water_drop.obj
Text to go here
10
10
I also tested the [three string][1] CreateElement() method with better but still not ideal results:
<MetaPipeObject:name xmlns:MetaPipeObject=“DogTest”>
water_drop.obj
Text to go here
10
10
</MetaPipeObject:name>
The result I am wanting:
water_drop.obj
Text to go here
10
10
[1]: XmlDocument.CreateElement Method (System.Xml) | Microsoft Learn