如何正确地从 innerXMl 获取值
Posted
技术标签:
【中文标题】如何正确地从 innerXMl 获取值【英文标题】:how to get the value from innerXMl correctly 【发布时间】:2014-05-27 00:38:24 【问题描述】:我计划检索数组中的每个建议,然后将其添加到列表中。我无法检索选项节点数据,在这种模式 aeroplane,aeroplanes, aerobian i 中,任何善良的灵魂都可以帮助我。
XmlDocument findStringDoc = new XmlDocument();
findStringDoc.Load (Application.dataPath+ "/" + filename);
XmlNodeList nodeList = findStringDoc.SelectNodes("/results/error");
//XmlNodeList suggestionNodeList = findStringDoc.SelectNodes("/results/error/suggestions/option");
foreach(XmlNode xn in nodeList)
errorString.Add(xn["string"].InnerText);
errorType.Add(xn["type"].InnerText);
//string temp = xn["suggestion"].InnerXml;
////TODO: Retrieve suggestions here!
XmlNodeList suggestionNodeList = findStringDoc.SelectNodes("/suggestions");
foreach(XmlNode yn in suggestionNodeList)
option[suggestionNodeList.Count] = yn["option"].InnerText;
Debug.Log(yn["option"].InnerText);
suggestionResult.Add (option);
//Debug.Log(suggestionResult);
//XmlNodeList suggestionNodeList = findStringDoc.SelectNodes("/results/error[string='0']/suggestions/option",errorString[i]);
<results>
<error>
<string>aeroplan</string>
<description>Spelling</description>
<precontext>a</precontext>
<suggestions>
<option>aeroplane</option>
<option>aeroplanes</option>
<option>aerobian</option>
</suggestions>
<type>spelling</type>
</error>
<error>
<string>i</string>
<description>Make I uppercase</description>
<precontext></precontext>
<suggestions>
<option>I</option>
</suggestions>
<type>grammar</type>
<url>http://service.afterthedeadline.com/info.slp?text=i&tags=PRP&engine=1</url>
</error>
</results>
【问题讨论】:
如果字符串tab值不唯一,会导致一些问题 【参考方案1】:您可以在下面使用我的代码:
private static void GetSuggestionOption(string filename, string value, string optionSuggest)
XDocument xDoc = XDocument.Parse(filename);
var parentNode = xDoc.Descendants().Where(x => x.Value == value).Ancestors().FirstOrDefault();
var childNode = parentNode.Descendants().Where(x => x.Name == optionSuggest);
childNode.ToList().ForEach(x => Console.WriteLine(x.Value));
调用:GetSuggestionOption(fileName, "aeroplan", "option");
编码愉快!
【讨论】:
很遗憾unity3d由于net.framework需要使用XMLDocument,无论如何感谢您的回答。【参考方案2】:您可以使用XmlDocument.SelectNodes()
方法传递合适的XPath 字符串参数来从XmlDocument
中选择特定元素,例如:
public void GetSuggestionOption(string keyword)
XmlDocument doc = new XmlDocument();
doc.Load (Application.dataPath+ "/" + filename);
string xpath = string.Format("//error[string='0']/suggestions/option", keyword);
XmlNodeList optionSuggestionList = doc.SelectNodes(xpath);
foreach (XmlNode option in optionSuggestionList)
Debug.Log(option.InnerXml);
您可以这样调用该方法,例如:GetSuggestionOption("aeroplan")
【讨论】:
是的,这正是我想要的。我想创建字符串列表的数量取决于错误的数量,并且在错误列表中,还有另一个内部列表存储错误建议选项。以上是关于如何正确地从 innerXMl 获取值的主要内容,如果未能解决你的问题,请参考以下文章
如何在 jsp 和 ajax 中为两个选择选项框动态地从 mysql 获取和填充值到 HTML 选择选项