简体   繁体   English

LINQ TO XML检索子元素值

[英]LINQ TO XML retrieving Child Element Value

I have the following XML 我有以下XML

<ABC xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ns.hr-xml.org/2007-04-15">
    <ReceiptId>
        <IdValue>123</IdValue>
    </ReceiptId>
    <ClientOrderId>
        <IdValue>345</IdValue>
    </ClientOrderId>
    <AccessPoint>
        <Description>My Description</Description>
    </AccessPoint>
    <ABCStatus>
        <Status>Error</Status>
        <Details>ERRORS:
 Talent is already in an active process for this opening.
        </Details>
        <StatusDate>2015-08-05</StatusDate>
    </ABCStatus>
</ABC>

I am trying to retrieve the element value 345 nested in IdValue and ClientOrderId 我正在尝试检索嵌套在IdValue和ClientOrderId中的元素值345

I have used the Linq to xml code in C# to retrieve the value with no luck 我已经在C#中使用Linq到xml代码来检索值,但是没有运气

XDocument XMLResults = XDocument.Parse(sResult);

var sClientOrderID =
        from nodeAElem in XMLResults.Root.Elements("ABC")
        from nodeA1Elem in nodeAElem.Elements("ClientOrderId")
        from nodeA11Elem in nodeA1Elem.Elements("IdValue")
        select nodeA11Elem.Value;

also need to retrieve the Status Elements value which is Error for the above xml. 还需要检索上述xml的“状态元素”值,即“错误”。

Any help is greatly appreciated 任何帮助是极大的赞赏

  1. Your XML document is using a namespace, you have to use it in your query to make it work. 您的XML文档使用的是名称空间,必须在查询中使用它才能使其正常工作。
  2. Root already brings you to ABC element, so you don't have to call Elements("ABC") Root已经将您带到ABC元素,因此您不必调用Elements("ABC")
  3. You're looking for single value, so you probably want to use Element instead of Elements . 您正在寻找单个值,因此您可能要使用Element而不是Elements
var ns = (XNamespace)"http://ns.hr-xml.org/2007-04-15";
var sClientOrderID = (int)XMLResults.Root
                                    .Element(ns + "ClientOrderId")
                                    .Element(ns + "IdValue");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM