none
System.Xml.Linq.XContainer.Element(…) returned null RRS feed

  • Question

  • Hi , it's my first time to manipulate XML file , I have an XML response from a web service and I want to get data inside an Xelement, I am using Linq to XML to make a request and store the data back to a list. but it always return null , I add the Xnamespace but it's allways the same problem.

    The XMl response :

    his XML file does not appear to have any style information associated with it. The document tree is shown below.
    <EstateServiceGetCountryListResponse xmlns="Whoman.Estate" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <CountryList>
    <EstateServiceGetCountryListResponseCountry>
    <CountryId>1</CountryId>
    <Name>Belgium</Name>
    </EstateServiceGetCountryListResponseCountry>
    <EstateServiceGetCountryListResponseCountry>...</EstateServiceGetCountryListResponseCountry>
    <EstateServiceGetCountryListResponseCountry>
    <CountryId>3</CountryId>
    <Name>France</Name>
    </EstateServiceGetCountryListResponseCountry>
    <EstateServiceGetCountryListResponseCountry>
    <CountryId>4</CountryId>
    <Name>Germany</Name>
    </EstateServiceGetCountryListResponseCountry>
    <EstateServiceGetCountryListResponseCountry>
    <CountryId>5</CountryId>
    <Name>Luxemburg</Name>

            </EstateServiceGetCountryListResponseCountry>

    <QueryInfo>

    <RowCount>248</RowCount>
    <Error i:nil="true"/>
    <Success>true</Success>
    </QueryInfo>
    </EstateServiceGetCountryListResponse>

    My Code :

    var xml = XDocument.Load("http://webservices.whoman2.be/websiteservices/EstateService.svc/GetCountryListXml?EstateServiceGetCountryListRequest={%22Language%22:%22en-GB%22}");

    XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";

    IEnumerable<string> Countries = from p in xml.Element(ns + "EstateServiceGetCountryListResponse")
                                                .Elements(ns + "CountryList")
                                                .Elements(ns+ "EstateServiceGetCountryListResponseCountry")
                                                select p.Element(ns + "CountryId").Value;

    after the execution it return me the exception System.Xml.Linq.XContainer.Element(…) returned null

    Thank you in advance for any help.

    Monday, December 9, 2019 5:30 PM

Answers

  • Try another namespace;

       XNamespace ns = "Whoman.Estate";

    An alternative:

    using System.Xml.XPath;
     
    . . .
     
    var m = new XmlNamespaceManager( new NameTable( ) );
    m.AddNamespace( "ns", "Whoman.Estate" );
    IEnumerable<string> country_ids = xml.XPathSelectElements( "/ns:EstateServiceGetCountryListResponse/ns:CountryList/ns:EstateServiceGetCountryListResponseCountry/ns:CountryId", m ).Select( e => e.Value );
     
    


    • Edited by Viorel_MVP Monday, December 9, 2019 6:44 PM
    • Marked as answer by salimi.NET Tuesday, December 10, 2019 4:16 AM
    Monday, December 9, 2019 6:36 PM

All replies

  • Try another namespace;

       XNamespace ns = "Whoman.Estate";

    An alternative:

    using System.Xml.XPath;
     
    . . .
     
    var m = new XmlNamespaceManager( new NameTable( ) );
    m.AddNamespace( "ns", "Whoman.Estate" );
    IEnumerable<string> country_ids = xml.XPathSelectElements( "/ns:EstateServiceGetCountryListResponse/ns:CountryList/ns:EstateServiceGetCountryListResponseCountry/ns:CountryId", m ).Select( e => e.Value );
     
    


    • Edited by Viorel_MVP Monday, December 9, 2019 6:44 PM
    • Marked as answer by salimi.NET Tuesday, December 10, 2019 4:16 AM
    Monday, December 9, 2019 6:36 PM
  • Thank you Mr Viorel , you save my life. 
    • Edited by salimi.NET Tuesday, December 10, 2019 4:18 AM
    Tuesday, December 10, 2019 4:15 AM