迭代的XElement并将其添加到父 [英] XElement Iteration and adding it to parent
问题描述
您好,我有以下问题......请你帮助,因为我很新的节目在C#中,没有编程经验...
hi I have the following question ...please do help as I am very new to programming in C# with no previous programming experience...
在我的代码我想通过在的XElement XML文件来迭代添加的XElement被称为能力的父母....这是我的代码...
In my code I am suppose to Iterate through XElement in a Xml file add that Xelement to a parent known as Capability....This is my code...
if (xElem.HasElements)
{
foreach (XElement xChild in xElem.Element("Elements"))
{
Capability capChild = Parse(xChild);
capParent.Children.Add(capChild);
}
}
这里的foreach循环给出错误
here the foreach loop gives an error
错误32 foreach语句无法在类型System.Xml.Linq.XElement'变量工作,因为System.Xml.Linq.XElement'不包含对于'的GetEnumerator'........
Error 32 foreach statement cannot operate on variables of type 'System.Xml.Linq.XElement' because 'System.Xml.Linq.XElement' does not contain a public definition for 'GetEnumerator'........
我怎么能进行调查,如果有的XElement的功能任何子并将其添加到母体能力?
how can I carry out the functionality of finding if XElement has any child and adding it to the parent Capability?
推荐答案
首先修复,添加一个s:
First fix, add an 's' :
//foreach (XElement xChild in xElem.Element("Elements"))
foreach (XElement xChild in xElem.Elements("Elements"))
这假定有1个或多个标记<元素>
在XML下xElem。
This assumes that there are 1 or more tags <Elements>
in your XML under xElem.
这篇关于迭代的XElement并将其添加到父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!