Software    csharp

I was writing some XLinq the other day, and an annoying thing happened. All of my queries yeilded no results. I started pulling out my hair and crying in a corner when I looked at the schema again and noticed a namespace declaration on the root element. -_-.

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfGeoPlaceDistance
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns="http://skats.net/services/literalTypes">
  <GeoPlaceDistance>
    <ToPlace>84142</ToPlace>
    <ToState>UT</ToState>
    <Distance>1</Distance>
  </GeoPlaceDistance>
</ArrayOfGeoPlaceDistance>

For those of you struggling with Xlinq on namespace declarations here is my codes.

// get the xml from a url that I have created
XElement doc = XElement.Load(url);
// grab the namespace declaration
XNamespace na = doc.GetDefaultNamespace();
// append it to all the queries
var elements = from e in doc.Elements(na + "GeoPlaceDistance")
               select e.Element(na + "ToPlace");

There I grabbed all the zip codes that were listed. I hope this helps someone.

Happy Coding.

comments powered by Disqus