From: omnistead on
This is my XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Profiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.mycompany.com/MyApp/2010/03/MySchema">
<Profile id="0" name="MyProfileName1">
<Entity name="EntityName1" type="EntityTypeName1">
<Shared>
<Property name="SharedPropertyName1" type="SharedPropertyType1" />
<Property name="SharedPropertyName2" type="SharedPropertyType2" />
</Shared>
<PropertyGroup name="PropertyGroupName1">
<Property name="PropertyName1" type="PropertyType1" />
</PropertyGroup>
<PropertyGroup name="PropertyGroupName2">
<Property name="PropertyName2" type="PropertyType2" />
</PropertyGroup>
</Entity>
</Profile>
<Profile id="1" name="MyProfileName2">
<Entity name="EntityName2" type="EntityTypeName2">
<PropertyGroup name="PropertyGroupName3">
<Property name="PropertyName3" type="PropertyType3" />
</PropertyGroup>
</Entity>
</Profile>
</Profiles>

I want to return all the "Profile" elements, and all the examples I have
seen show one of two syntax:

static void Main(string[] args)
{
Program program = new Program();
IEnumerable<XElement> profiles =
program.GetProfiles(XDocument.Load("Profile.xml"));

Console.WriteLine(profiles.Count());
Console.ReadKey();

profiles = program.GetProfiles2(XDocument.Load("Profile.xml"));

Console.WriteLine(profiles.Count());
Console.ReadKey();
}

private IEnumerable<XElement> GetProfiles(XDocument document)
{
return document.Descendants("Profile");
}

private IEnumerable<XElement> GetProfiles2(XDocument document)
{
return document.Element("Profiles").Elements("Profile");
}


The first method return 0; the second method throws a NullReferenceException
when trying to get the Profiles element. This really shouldn't be this
difficult.

From: Peter Duniho on
omnistead wrote:
> [...]
> The first method return 0; the second method throws a
> NullReferenceException when trying to get the Profiles element. This
> really shouldn't be this difficult.

I haven't tested the code you posted. But looking at it, it appears you
have the classic "forgot the namespace" bug.

Your XML sets a default namespace for the content within the <Profiles>
element (including that element). So, while there's no explicit
namespace for each XML element name, they _do_ have a namespace, and if
you don't specify the namespace, you won't find the element.

So, the fix would be to provide a name that has the correct namespace.
Try doing that and see if that helps.

Pete
From: omnistead on
That was it - I created a XNamespace property, set it, prepended it to the
element names and everything works.

Thanks!

"Peter Duniho" <no.peted.spam(a)no.nwlink.spam.com> wrote in message
news:uIaO2NwwKHA.2436(a)TK2MSFTNGP04.phx.gbl...
> omnistead wrote:
>> [...]
>> The first method return 0; the second method throws a
>> NullReferenceException when trying to get the Profiles element. This
>> really shouldn't be this difficult.
>
> I haven't tested the code you posted. But looking at it, it appears you
> have the classic "forgot the namespace" bug.
>
> Your XML sets a default namespace for the content within the <Profiles>
> element (including that element). So, while there's no explicit namespace
> for each XML element name, they _do_ have a namespace, and if you don't
> specify the namespace, you won't find the element.
>
> So, the fix would be to provide a name that has the correct namespace. Try
> doing that and see if that helps.
>
> Pete