Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Firends,
My requirement is: I need to traverse through my xml and apply some search criteria. and save the result in a List.
My problem is : I not able to select all positions node by Descendants keyword.
Below is my required path
Envelope->Body->GetRequestedDataResponse->GetRequestedDataResult->Message->EBCMessageType->Message->NewDataSet->Positions
I am stuck with here var ac = doc.Root.Descendants("Positions")
please help.

My XML
XML
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetRequestedDataResponse xmlns="http://tempuri.org/">
      <GetRequestedDataResult>
        <ReferenceID xmlns="http://ebc.mssb.com">M99TzPSsNsKxIxfUfhG</ReferenceID>
        <ReceivedTime xmlns="http://ebc.mssb.com">2015-07-17T10:18:40.6321762-04:00</ReceivedTime>
        <CompletedTime xmlns="http://ebc.mssb.com">2015-07-17T10:18:41.4491762-04:00</CompletedTime>
        <StatusCode xmlns="http://ebc.mssb.com">Success</StatusCode>
        <Message xmlns="http://ebc.mssb.com">
          <EBCMessageType>
            <Message>{ CALL GETMM_ACCT_TURBPOS (RSGETMM_ACCT_TURBPOSFAWorkstation}</Message>
            <MessageType>COMMAND_STRING</MessageType>
          </EBCMessageType>
          <EBCMessageType>
            <Message>
              <NewDataSet>
              <Positions>
              <Office>101</Office>
              <Account>10239</Account>
              <AccruedInterest>25.32</AccruedInterest>
              <Cusip>asd</Cusip>
              <PriceFactor>1.0</PriceFactor>
               <Quantity>5</Quantity>
              </Positions>
                <Positions>
                  <Office>101</Office>
                  <Account>10269</Account>
                  <AccruedInterest>45.34</AccruedInterest>
                  <Cusip>pqr</Cusip>
                  <PriceFactor>1.0</PriceFactor>
                  <Quantity>45</Quantity>
                </Positions>            
              </NewDataSet>
            </Message>
            <MessageType>RAW_DATA</MessageType>
          </EBCMessageType>
        </Message>
        
      </GetRequestedDataResult>
    </GetRequestedDataResponse>
  </s:Body>
</s:Envelope>


c# code
C#
public static List<AccruedClass> validaccrued()
       {
           XDocument doc = XDocument.Load("SampleXML.xml");
           var ac = doc.Root.Descendants("Positions")
                        .Where(item => (float?)item.Element("AccruedInterest") != null && (float?)item.Element("AccruedInterest") > 0.0)
                         .Select(item => new AccruedClass
                         {
                             Cusip = item.Element("Cusip").Value != null ? item.Element("Cusip").Value : "",
                             // Symbol = item.Element("symbol").Value,
                             AccruedInterest = item.Element("AccruedInterest").Value != null ? Convert.ToDouble(item.Element("AccruedInterest").Value) : 0,
                             PriceFactor = item.Element("PriceFactor").Value != null ? Convert.ToDouble(item.Element("PriceFactor").Value) : 0,
                             Quantity = item.Element("Quantity").Value != null ? Convert.ToDouble(item.Element("Quantity").Value) : 0,
                         })
                         .ToList();
           List<AccruedClass> list_accr = ac.ToList<AccruedClass>();
           return list_accr;
       }


C#
static void Main(string[] args)
       {
           List<AccruedClass> ac = new List<AccruedClass>();
           ac = validaccrued();
       }


C#
public class AccruedClass
   {
       public string Cusip { get; set; }
       // public string Symbol { get; set; }
       public double AccruedInterest { get; set; }
       public double PriceFactor { get; set; }
       public double Quantity { get; set; }
       public double CalcAcc { get; set; }
   }
Posted
Updated 17-Jul-15 9:51am
v2

1 solution

The problem is that the "Positions" elements are in a namespace, inherited from the enclosing <Message xmlns="http://ebc.mssb.com"> so change to include the namespace info:
C#
public static List<AccruedClass> validaccrued()
        {
            XDocument doc = XDocument.Load("SampleXML.xml");
            // This next string expression SHOULD be a single string
            // but CodeProject tries to be too smart and thinks it should be turned into an active link!
            XNamespace ns = "http"+"://ebc.mssb.com";   // assuming it doesn't vary from file to file...
            var ac = doc.Root.Descendants(ns + "Positions")
                         .Where(item => {
                                  float? accInt = (float?)item.Element("AccruedInterest");
                                  return accInt != null && accInt.Value > 0.0f;
                                })
                         // etc, same as before...
 
Share this answer
 
v4
Comments
jinesh sam 18-Jul-15 1:18am    
I am not getting the result. What about other namespaces. Shall I add that to the namemanager.
jinesh sam 18-Jul-15 1:28am    
if i remove the namespace everything works fine
Matt T Heffron 20-Jul-15 11:55am    
If the issue is solved, please formally "Accept" this solution.
jinesh sam 23-Jul-15 6:50am    
@Matt Its not working with namespaces.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900