LINQ to XML - what am I doing wrong

  • Thread starter Thread starter omnistead
  • Start date Start date
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.
 
omnistead said:
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.

LINQ stand for Language-Integrated Query, and the operative word there
is *Query*.


<http://www.mssqltips.com/tip.asp?tip=1524>

// Create the query
var custs = from c in XElement.Load("Customers.xml").Elements("Customers")
select c ;

// Execute the query
foreach (var customer in custs)
{
Console.WriteLine(customer);
}


//Pause the application
Console.ReadLine();


<http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx>

Code:
// Loading from a file, you can also load from a stream
XDocument loaded = XDocument.Load(@"C:\contacts.xml");


// Query the data and write out a subset of contacts
var q = from c in loaded.Descendants("contact")
where (int)c.Attribute("contactId") < 4
select (string)c.Element("firstName") + “ “ +
(string)c.Element("lastName");


foreach (string name in q)
Console.WriteLine("Customer name = {0}", name);


Output:
Customer name = Barney Gottshall
Customer name = Armando Valdes
 
Back
Top