我正在尝试为Unity应用程序(用Unityscript编写)编写脚本,该脚本能够在运行时将节点添加到XML文档(使用XmlDocument类)。 我在尝试声明新节点的' 属性时遇到了问题。
我想知道如何使用默认XMLNS属性之外的命名空间创建节点(在我的情况下,将' xmlns =' 替换为 ' name =' )以匹配现有标记。
在MSDN文档上花了很长时间后,我认为这可能与 XmlNameSpaceManager 或更改默认命名空间有关,但是我很难理解如何实现这一点(在XML中仍然很新) Unity / uJS)所以任何建议都会非常受欢迎。
非常感谢,
赖安
当前代码:
function Start(){
//Automatically loads XML doc for save etc
doc = new XmlDocument();
doc.Load(Application.dataPath + "/objMetaTest01.xml");
root = doc.DocumentElement;
Debug.Log("XML Root: " + root.Name);
nodeList = root.SelectNodes("MetaPipeObject");
Debug.Log("***XML Log Begin ***");
Debug.Log("Number of Objects: " + nodeList.Count); //returns total number of MPObjs
for (var i = 0; i < nodeList.Count; i++)
{
var node = nodeList[i];
//Debug.Log("Node Name: " + node.Attributes["name"].Value);
}
//Namespace Manager to add namespace to file
//docNameSpace = new XmlNamespaceManager(doc.NameTable);
//docNameSpace.AddNamespace("name", doc.DocumentElement.NamespaceURI);
//Debug.Log("Doc DefaultNamespace: " + docNameSpace.DefaultNamespace);
//Debug.Log("nmsg has 'name' prefix: " + docNameSpace.HasNamespace("name"));
Debug.Log("***XML Log End ***");
}
public function CreateNewNode(){
//Creates new node for new objects
//will be similar to the replace function to be written soon
//select last MP node to add the new one behind
doc.DocumentElement.RemoveAttribute("xlmns");
var lastObjNode = root.SelectSingleNode("MetaPipeObject[last()]");
var newObjNode = doc.CreateElement("MetaPipeObject", "DogTest");
//Create new attribute test
//var nameAttr = doc.CreateAttribute("name");
//newObjNode.Attributes.Append(nameAttr);
//"name", "nameTest", doc.DocumentElement.NamespaceURI, doc
newObjNode.InnerXml = lastObjNode.InnerXml; //copy content
root.InsertAfter(newObjNode, lastObjNode); //add to the bottom of the xml doc
doc.Save(Application.dataPath + "/objMetaTest01.xml");
Debug.Log("New Nodes Attribute: " + newObjNode.Attributes);
Debug.Log("New Node Namespace: " + newObjNode.NamespaceURI);
Debug.Log("New Node Name: " + newObjNode.Name);
Debug.Log("New node is: " + newObjNode.InnerText);
}
&#13;
目前退货
<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 name="DogTest">
<FileName>water_drop.obj</FileName>
<Description>Text to go here</Description>
<Health>10</Health>
<Experience>10</Experience>
答案 0 :(得分:0)
请查看以下链接以获取帮助
答案 1 :(得分:0)
经过进一步研究后发现,创建所需XML格式的解决方案比我最初研究的路线要简单得多,而且要简单得多。
我希望这有助于任何发现自己处于类似情况的人。 NamspaceManager是我可以使用的另一种可能的路由,并且分配了除默认值之外的不同命名空间,但是为了解决我的问题,这似乎有点过头了。
以下是我最终使用的代码片段:
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"));
}