Xelement搜索

时间:2011-08-30 16:56:02

标签: c# .net

变量resp具有以下值。 System.Xml.Linq.XElement resp - 如何在C#中获取Statuscode的值?

谢谢!

** resp的值是(不包括整个xml,它很长):

<?xml version="1.0" encoding="utf-8" ?>
<fiAPI xmlns="http://integration.fiapi.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xsi:schemalocation="http://integration.fiapi.com fiDocumentInquiry.xsd">
  <fiHeader Version="2.0">
    <Service Version="1.0" Name="FiservArchiveAccessAPI">
      <DateTime>2011-08-29T02:27:21-0-4:00</DateTime>
      <UUID>12345678-1234-1234-1234-123456789012</UUID>
    </Service>
    <Security>
      <AuthenticationMaterial>
        <PrincipalPWD></PrincipalPWD>
      </AuthenticationMaterial>
      <PrincipalID></PrincipalID>
    </Security>
    <Client Version="1.3">
      <VendorID>Vendor Name</VendorID>
      <AppID>CWS</AppID>
      <OrgID>44444451</OrgID>
      <SessionID>88888888-4444-4444-4444-123456789012</SessionID>
    </Client>
    <DataSource>
      <URI />
    </DataSource>
  </fiHeader>
  <Response TypeOfResponse="DocumentInquiryRs" TotalRows="2">
    <Status>
      <StatusCode>0</StatusCode>
      <Severity />
    </Status>
    <RequestEcho TypeOfRequest="DocumentInquiryRq" RequestID="123" Echo="True">

1 个答案:

答案 0 :(得分:2)

您可以使用Descendants方法。因为您的XML具有命名空间,所以请确保在查询时包含它:

class Program
{
    static void Main()
    {
        var resp = XElement.Load("test.xml");
        var statusCode = resp
            .Descendants("{http://integration.fiapi.com}StatusCode")
            .FirstOrDefault();
        if (statusCode != null)
        {
            Console.WriteLine(statusCode.Value);
        }
    }
}