none
Linq to XML

    Question

  • Could any one help me to find the below heighted in bold, italic and underline key from below XML? As I am new to Linq to XML.

    It will be good if any one can provide me any hit so that I can work in that directions.

    Thanks in Advance.

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.test/envelope/">
      <soapenv:Header xmlns:kd4="</soapenv:Header">http://www.test.com/"></soapenv:Header>
      <soapenv:Body>
        <shop:SearchRsp TraceId="SP1PAS635327267325794452" xmlns:shar="http://www.test.com"  xmlns:air="http://www.test1.com" xmlns:shop="http://www.test2.com">
          <shar:ResponseMessage Code="701832" Type="Warning">TEST Message</shar:ResponseMessage>
          <shop:AirSegmentList>
            <air:AirSegment Key="400001T" Group="0" Carrier="F9" FlightNumber="427" Origin="ATL" Destination="DEN" DepartureTime="2014-05-15T19:08:00.000-04:00" ArrivalTime="2014-05-15T20:25:00.000-06:00" >
              <air:Equipment Code="319" ChangeOfPlane="false"/>
              <air:AirAvailInfo >
                <air:BookingCodeInfo BookingCounts="W7"/>
              </air:AirAvailInfo>
              <air:FlightDetailsRef Key="400000T"/>
            </air:AirSegment>
            <air:AirSegment Key="400006T" Group="0" Carrier="F9" FlightNumber="429" Origin="ATL" Destination="DEN" DepartureTime="2014-05-15T09:54:00.000-04:00" ArrivalTime="2014-05-15T11:10:00.000-06:00" >
              <air:Equipment Code="319" ChangeOfPlane="false"/>
              <air:AirAvailInfo>
                <air:BookingCodeInfo BookingCounts="W7"/>
              </air:AirAvailInfo>
              <air:FlightDetailsRef Key="400005T"/>
            </air:AirSegment>
          </shop:AirSegmentList>
        </shop:SearchRsp>
      </soapenv:Body>
    </soapenv:Envelope>

    Friday, April 11, 2014 2:10 AM

Answers

  • Gaur : Hope this helps.. Change accordingly based on your namespace..

        XDocument objDoc = XDocument.Load(@"C:\Sites\stringEx\data.xml");//XDocument.Parse("your string")
            XNamespace objNS = "http://www.test1.com";
            Console.WriteLine(" Flight# = {0}", objDoc.Descendants(objNS + "AirSegment").Attributes("FlightNumber").FirstOrDefault().Value);
            Console.WriteLine(" BookingCounts = {0}", objDoc.Descendants(objNS + "BookingCodeInfo").Attributes("BookingCounts").FirstOrDefault().Value);
         

    • Marked as answer by Gaur3 Wednesday, April 16, 2014 4:12 AM
    Friday, April 11, 2014 4:15 PM

All replies

  • Hello,

    For LINQ2XML, you can check this link:

    http://msdn.microsoft.com/en-us/library/bb387098.aspx

    In your case, I suggest you using the XPath in LINQ2XML:

    http://msdn.microsoft.com/en-us/library/bb675178.aspx

    For example, we want to get the value: 427 of the arrtibutte: FlightNumber, we can write it as:

    XDocument xDoc = XDocument.Load(@"D:\BMX\SourceFile\2014\04\S11.xml");
    var xElement = xDoc.XPathSelectElement("//*[local-name() = 'AirSegment']").Attribute("FlightNumber").Value;
    

    The “//*”, when you use the XPathSelectElement(), it will return the first found node in XML file, while you use XPathSelectElements(), method, it will return all nodes whose name is AirSegment.

    The “local-name()” is used to ignore the namespace in XML file.

    You can have a try.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 11, 2014 7:20 AM
    Moderator
  • Gaur : Hope this helps.. Change accordingly based on your namespace..

        XDocument objDoc = XDocument.Load(@"C:\Sites\stringEx\data.xml");//XDocument.Parse("your string")
            XNamespace objNS = "http://www.test1.com";
            Console.WriteLine(" Flight# = {0}", objDoc.Descendants(objNS + "AirSegment").Attributes("FlightNumber").FirstOrDefault().Value);
            Console.WriteLine(" BookingCounts = {0}", objDoc.Descendants(objNS + "BookingCodeInfo").Attributes("BookingCounts").FirstOrDefault().Value);
         

    • Marked as answer by Gaur3 Wednesday, April 16, 2014 4:12 AM
    Friday, April 11, 2014 4:15 PM
  • Thanks for your inputs.

    Below is the final query that may help others too.

    XNamespace shop = @"http://www.test.com/"; /* Any namespace as per the XML schema */
    XNamespace air = @"http://www.test_1.com/";
    XNamespace com = @"http://www.test_2.com/";

    var airSegment = (from segLst in SearchRspList
                                                    .Descendants(shop + "SearchRsp")
                                                    .Descendants(shop + "AirSegmentList")
                                                    .Elements(air + "AirSegment")
                                                    .Where(n => n.Attribute("FlightNumber").Value == "1234")
                                                    select new
                                                    {
                                                        Key = segLst.Attribute("Key").Value,
                                                        Origin = segLst.Attribute("Origin").Value,
                                                        Departure = segLst.Attribute("Destination").Value,
                                                        Carrier = segLst.Attribute("Carrier").Value
                                                    }
                                               ).FirstOrDefault();


    • Edited by Gaur3 Wednesday, April 16, 2014 4:15 AM
    Wednesday, April 16, 2014 4:12 AM