[英]Get InnerText from Collection
當節點在集合中時,有沒有辦法獲取節點的內部文本目前我有這個
Collection<string> DependentNodes = new Collection<string>();
foreach (XmlNode node in nodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
DependentNodes.Add(node.ChildNodes[i].InnerXml);
//the reason i'm using InnerXml is that it will return all the child node of testfixture in one single line,then we can find the category & check if there's dependson
}
}
string selectedtestcase = "abc_somewords";
foreach (string s in DependentNodes)
{
if(s.Contains(selectedtestcase))
{
MessageBox.Show("aaa");
}
}
當我調試字符串 s 或索引里面有這個時[在一行中]
<testfixture name="1" description="a">
<categories>
<category>abc_somewords</category>
</categories>
<test name="a" description="a">
<dependencies>
<dependson typename="dependsonthis" />
</dependencies>
</test>
</testfixture>
我要做的是當我們到達“testfixture 1”時,它會找到“abc_somewords”並搜索“dependson typename”節點(如果有的話)並獲得“typename”(即“dependonthis”)。
你能用 linq 到 xml 嗎? 像下面這樣的東西可能是一個不錯的開始
xml.Elements("categories").Where(x => x.Element("category").Value.Contains(selectedtestcase));
這不是我的想法,所以可能需要改進
PS 使用 XElement.Load 或 XElement.Parse 將您的 xml 放入 XElements
由於您已經使用XmlNode
,您可以使用 XPath 表達式來 select 所需的textfixture
節點,以及 select 依賴值:
XmlDocument doc = // ...
XmlNode node = doc.SelectSingleNode("//testfixture[contains(categories/category, \"abc\")]/test/dependencies/dependson/");
if (node != null)
{
MessageBox.Show(node.Attributes["typename"]);
}
這將選擇屬於category
包含“abc”的dependson
節點的testfixture
節點。 node.Attributes["typename"]
將返回typename
屬性的值。
編輯:將 XPath 表達式更新為更具體的問題信息
假設
當您在代碼中循環並想要創建一個集合時,我假設實際的 Xml 文件內部有幾個testfixture
節點,例如下面的假設示例:
<root>
<testfixture name="1" description="a">
<categories>
<category>abc_somewords</category>
</categories>
<test name="a" description="a">
<dependencies>
<dependson typename="dependsonthis" />
</dependencies>
</test>
</testfixture>
<testfixture name="2" description="a">
<categories>
<category>another_value</category>
</categories>
<test name="b" description="a">
<dependencies>
<dependson typename="secondentry" />
</dependencies>
</test>
</testfixture>
<testfixture name="3" description="a">
<categories>
<category>abc_somewords</category>
</categories>
<test name="c" description="a">
<dependencies>
<dependson typename="thirdentry" />
</dependencies>
</test>
</testfixture>
</root>
使用 Linq 到 Xml 的代碼
要使用 Linq,您必須引用以下名稱空間:
using System.Linq;
using System.Xml.Linq;
在上面假設的 xml 文件結構上使用 Linq To Xml 文件結構如下所示:
// To Load Xml Content from File.
XDocument doc1 = XDocument.Load(@"C:\MyXml.xml");
Collection<string> DependentNodes = new Collection<string>();
var results =
doc1.Root.Elements("testfixture")
.Where(x => x.Element("categories").Element("category").Value.Contains("abc_somewords"))
.Elements("test").Elements("dependencies").Elements("dependson").Attributes("typename").ToArray();
foreach (XAttribute attribute in results)
{
DependentNodes.Add(attribute.Value.Trim());
}
結果
生成的集合將包含以下內容:
如您所見,僅提取了typename
屬性的文本,其中testfixture
dependson
中,該節點包含值為abc_somewords
的category
節點。
補充說明
如果你從一個字符串中讀取 xml 你也可以使用這個:
// To Load Xml Content from a string.
XDocument doc = XDocument.Parse(myXml);
如果您的完整Xml 結構不同,請隨時發布,我會更改代碼以匹配。
玩得開心。
我不知道您使用的是什么“節點”。
這是符合您要求的代碼(我理解的)。
Collection<XmlNode> DependentNodes = new Collection<XmlNode>();
XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"Path_Of_Your_xml");
foreach (XmlNode node in xDoc.SelectNodes("testfixture")) // Here I am accessing only root node. Give Xpath if ur requrement is changed
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
DependentNodes.Add(node.ChildNodes[i]);
}
}
string selectedtestcase = "abc_somewords";
foreach (var s in DependentNodes)
{
if (s.InnerText.Contains(selectedtestcase))
{
Console.Write("aaa");
}
}
using System;
using System.Xml;
namespace ConsoleApplication6
{
class Program
{
private const string XML = "<testfixture name=\"1\" description=\"a\">" +
"<categories>" +
"<category>abc_somewords</category>" +
"</categories>" +
"<test name=\"a\" description=\"a\">" +
"<dependencies>" +
"<dependson typename=\"dependsonthis\" />" +
"</dependencies>" +
"</test>" +
"</testfixture>";
static void Main(string[] args)
{
var document = new XmlDocument();
document.LoadXml(XML);
var testfixture = document.SelectSingleNode("//testfixture[@name = 1]");
var category = testfixture.SelectSingleNode(".//category[contains(text(), 'abc_somewords')]");
if(category != null)
{
var depends = testfixture.SelectSingleNode("//dependson");
Console.Out.WriteLine(depends.Attributes["typename"].Value);
}
Console.ReadKey();
}
}
}
Output:取決於這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.