O
omnistead
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.
<?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.