System.Xml.Linq.XContainer.Element(…) returned null

  • Thread starter Thread starter salimi.NET
  • Start date Start date
S

salimi.NET

Guest
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/websi...ryListXml?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.

Continue reading...
 
Back
Top