[英]c# Elements of elements using XElement
不知道我該怎么做。
這是我正在使用的 XML:
<configuration>
<tag1>
<interestingstuff>avalue</interestingstuff>
</tag1>
<tag2>
<item_type1 name="foo">
<item_type2 name="bar">
<property>value1</property>
<property>value2</property>
</item_type2>
<item_type2 name="pub">
<property>valueX</property>
<property>valyeY</property>
</item_type2>
<item_type1>
<item_type1 name="foo2">
<item_type2 name="pub">
<property>valueX</property>
</item_type2>
</item_type1>
</tag2>
</configuration>
我正在編寫一個函數,該函數傳遞 item_type 和 item_type2 的值,並返回該組合的屬性值列表。
這就是我所擁有的。 它在指出的點拋出“對象未設置為對象的實例”異常。
ArrayList properties = new ArrayList();
XDocument config = new XDocument();
config = XDocument.Parse(XML_Text);
foreach (XElement item1 in config.Root.Element("tag2").Nodes())
{
if (item1.Attribute("name").Value.ToString() == passed_item1_value)
{
//this is where it's breaking
foreach (XElement item2 in item1.Elements("item_type2").Nodes())
{
if item2.Attribute("name").Value.ToString() == passed_item2_value)
{
foreach (XElement property in item2.Elements("property").Nodes())
{
properties.Add(property.Value.ToString());
}
break;
}
}
break;
}
}
我知道這沒有意義 - 但我無法讓它有意義。
我會這樣做:
public IEnumerable<string> findValues(string item1, string item2)
{
config = XDocument.Parse(XML_Text)
var res = config.Descendants("item_type1")
.Where(x=>x.Attribute("name").Value == item1)
.Descendants("item_type2")
.Where(x=>x.Attribute("name").Value == item2);
return res.Descendants("property").Select(x=>x.Value);
}
我猜你想要一些帶有XPath查詢的東西,如下所示:
var path = string.Join("/",
"configuration",
"tag2",
string.Format("item_type1[@name='{0}']", passed_item1_value),
string.Format("item_type2[@name='{0}']", passed_item2_value),
"property");
var elements = (IEnumerable)config.XPathEvaluate(path);
var properties = elements.Cast<XElement>().Select(x => x.Value);
不要忘記包含using System.Xml.XPath;
這里定義了XPathEvaluate
。
using System.Linq;
using System.Xml.Linq;
IEnumerable<string> GetProperties(XElement xml, string item1, string item2)
{
return xml.Element("tag2")
.Elements("item_type1").Where(x => x.Attribute("name").Value == item1)
.Elements("item_type2").Where(x => x.Attribute("name").Value == item2)
.SelectMany(x => x.Elements("property").Select(p => p.Value));
}
與上述許多解決方案類似,但使用數據的直接路徑並使用SelectMany
將結果轉換為單個集合 - 即如果您有 item1 和 item2 的重復項,它將起作用。
如果 VB'er 需要類似的幫助
Dim xe As XElement
xe = <configuration>
<tag1>
<interestingstuff>avalue</interestingstuff>
</tag1>
<tag2>
<item_type1 name="foo">
<item_type2 name="bar">
<property>value1</property>
<property>value2</property>
</item_type2>
<item_type2 name="pub">
<property>valueX</property>
<property>valyeY</property>
</item_type2>
</item_type1>
<item_type1 name="foo2">
<item_type2 name="pub">
<property>valueX</property>
</item_type2>
</item_type1>
</tag2>
</configuration>
Dim passed_item1_value As String = "foo" 'for test
Dim passed_item2_value As String = "pub" 'for test
Dim ie As IEnumerable(Of String)
ie = (From el In xe.<tag2>.<item_type1>...<item_type2>
Where el.Parent.@name = passed_item1_value AndAlso el.@name = passed_item2_value
From sel In el...<property> Select sel.Value)
Return ie.ToArray
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.