[英]Checking the name and value of an XML node, using LINQ
I have a C# application where I need to parse XML using LINQ. 我有一个C#应用程序,需要在其中使用LINQ解析XML。 This is my first LINQ experience that's why I am struggling with basic operations 这是我第一次使用LINQ,这就是我在基本操作方面苦苦挣扎的原因
My XML looks something similar to: 我的XML看起来类似于:
<Main>
<Data>
<NodeTypeA>
<ElementA>23</ElementA>
<ElementB>24</ElementB>
</NodeTypeA>
</Data>
</Main>
So first I want to check the name of the first child of “Data”. 所以首先我要检查“数据”的第一个孩子的名字。 In this case it is “NodeTypeA”. 在这种情况下,它是“ NodeTypeA”。
Second I want to read the value of ElementA value. 其次,我想读取ElementA值的值。 In this example it is “23” 在此示例中为“ 23”
You can do the following: 您可以执行以下操作:
var firstElement = xml.Descendants("Data").Elements().FirstOrDefault();
if (firstElement != null && firstElement.Name == "NodeTypeA")
{
var elementAValue = (string)firstElement.Element("ElementA");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.