C# Unity中的XML对话树未正确解析信息
我曾尝试使用XML在unity中建立一个对话树(我以前很少使用XML,所以我不确定我的方法是否正确) 因此,我试图从这个对话树中获取第一个文本元素,但当我调用XML文件并说出它在哪里时,我获取了存储在该分支中的所有内容 我是否使用了正确的.XML来实现这一点,正如我看到人们说的那样,使用.XML.LINQ或.XML.Serialization而不仅仅是.XML对我的情况正确吗 代码:C# Unity中的XML对话树未正确解析信息,c#,xml,unity3d,C#,Xml,Unity3d,我曾尝试使用XML在unity中建立一个对话树(我以前很少使用XML,所以我不确定我的方法是否正确) 因此,我试图从这个对话树中获取第一个文本元素,但当我调用XML文件并说出它在哪里时,我获取了存储在该分支中的所有内容 我是否使用了正确的.XML来实现这一点,正如我看到人们说的那样,使用.XML.LINQ或.XML.Serialization而不仅仅是.XML对我的情况正确吗 代码: 使用UnityEngine; 使用系统集合; 使用System.IO; 使用System.Xml; 使用Unit
使用UnityEngine;
使用系统集合;
使用System.IO;
使用System.Xml;
使用UnityEngine.UI;
使用System.Collections.Generic;
公共类对话框树
{
公共字符串文本;
公共列表文本;
公共列表节点;
public void parseXML(字符串xmlData)
{
XmlDocument xmlDoc=新的XmlDocument();
Load(新的StringReader(xmlData));
XmlNode node=xmlDoc.SelectSingleNode(“dialoguetree/dialoguebranch”);
text=node.InnerXml;
XmlNodeList myNodeList=xmlDoc.SelectNodes(“dialoguebranch/dialoguebranch”);
foreach(myNodeList中的XMLNode1)
{
如果(node1.InnerXml.Length>0)
{
DialogTree dialogtreenode=新建DialogTree();
dialogtreenode.parseXML(node1.InnerXml);
添加(dialogtreenode);
}
}
}
}
下面是XML的图片。
因此,我试图抓住文本的第一个元素,然后在后面的响应中,它将转到分支1或分支2
<?xml version='1.0'?>
<dialoguetree>
<dialoguebranch>
<text>Testing if the test prints</text>
<dialoguebranch>
<text>Branch 1</text>
<dialoguebranch>
<text>Branch 1a</text>
</dialoguebranch>
<dialoguebranch>
<text>Branch 1b</text>
</dialoguebranch>
</dialoguebranch>
<dialoguebranch>
<text>Branch 2</text>
</dialoguebranch>
</dialoguebranch>
</dialoguetree>
测试测试是否打印
分支机构1
分支机构1a
分支机构1b
分支机构2
您将获得该分支中的所有内容,因为XmlNode.InnerXML
返回该节点中的所有内容。有关这方面的更多信息,请参阅
您应该使用分支
作为只查看其子级的基础,而不是每次都从xmlDoc
开始。另外,您需要一个入口点来进入第一个dialoguetree
元素,然后忽略它。最后,我只创建一个XmlDocument
,并在递归中传递节点
总之,这可能看起来像这样:
public class DialogTree
{
public string text;
public List<DialogTree> nodes = new List<DialogTree>();
public static DialogTree ParseXMLStart(string xmlData)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new Stringreader(xmlData));
XmlNode rootNode = xmlDoc.SelectSingleNode("dialoguetree/dialoguebranch");
DialogTree dialogTree = new DialogTree();
dialogTree.ParseXML(rootNode);
return dialogTree;
}
public void ParseXML(XmlNode parentNode)
{
XmlNode textNode = parentNode.SelectSingleNode("text");
text = textNode.InnerText;
XmlNodeList myNodeList = parentNode.SelectNodes("dialoguebranch");
foreach (XmlNode curNode in myNodeList)
{
if (curNode.InnerXml.Length > 0)
{
DialogTree dialogTree = new DialogTree();
dialogTree.ParseXML(curNode);
nodes.Add(dialogTree);
}
}
}
}
所有这些代码都未经测试,但我希望总体思路是明确的。如果您在下面的注释中发现任何错误,请告诉我,我将尝试修复它们。您将获得该分支中的所有内容,因为
XmlNode.InnerXML
返回该节点中的所有内容。有关这方面的更多信息,请参阅
您应该使用分支
作为只查看其子级的基础,而不是每次都从xmlDoc
开始。另外,您需要一个入口点来进入第一个dialoguetree
元素,然后忽略它。最后,我只创建一个XmlDocument
,并在递归中传递节点
总之,这可能看起来像这样:
public class DialogTree
{
public string text;
public List<DialogTree> nodes = new List<DialogTree>();
public static DialogTree ParseXMLStart(string xmlData)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new Stringreader(xmlData));
XmlNode rootNode = xmlDoc.SelectSingleNode("dialoguetree/dialoguebranch");
DialogTree dialogTree = new DialogTree();
dialogTree.ParseXML(rootNode);
return dialogTree;
}
public void ParseXML(XmlNode parentNode)
{
XmlNode textNode = parentNode.SelectSingleNode("text");
text = textNode.InnerText;
XmlNodeList myNodeList = parentNode.SelectNodes("dialoguebranch");
foreach (XmlNode curNode in myNodeList)
{
if (curNode.InnerXml.Length > 0)
{
DialogTree dialogTree = new DialogTree();
dialogTree.ParseXML(curNode);
nodes.Add(dialogTree);
}
}
}
}
所有这些代码都未经测试,但我希望总体思路是明确的。如果您在下面的评论中发现任何错误,请告诉我,我将尝试修复它们
string xmlStringFromFile;
DialogTree dialogue = DialogTree.ParseXMLStart(xmlStringFromFile);