Reputation: 69
I am reading data from XML like this: (Contacts.xml)
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--LINQ to XML Contacts XML Example-->
<?MyApp 123-44-4444?>
<contacts>
<contact>
<name>Patrick Hines</name>
<phone type="home">206-555-0144</phone>
<phone type="work">425-555-0145</phone>
<address>
<street1>123 Main St</street1>
<city>Mercer Island</city>
<state>WA</state>
<postal>68042</postal>
</address>
</contact>
<contact>
<name>Gretchen Rivas</name>
<phone type="mobile">206-555-0163</phone>
<address>
<street1>123 Main St</street1>
<city>Mercer Island</city>
<state>WA</state>
<postal>68042</postal>
</address>
</contact>
</contacts>
My code for getting data from XML file: (contacts.cs)
public class Contact
{
//static members
public static string fLocation;
//private members
private string name;
private List<PhoneNumber> pNumber;
private Adress cAdress;
//public members
public Adress CAdress
{
get { return cAdress; }
}
public List<PhoneNumber> PNumber
{
get { return pNumber; }
}
public string Name
{
get { return name; }
}
//Constructor
public Contact(string _name, List<PhoneNumber> _pNumber,Adress _cAdress)
{
name = _name;
pNumber = _pNumber;
cAdress = _cAdress;
}
public static List<Contact> Get()
{
List<Contact> output = new List<Contact>();
XDocument data = XDocument.Load(fLocation);
var query = from c in data.Descendants("contact")
orderby c.Element("name").Value
select c;
foreach (var item in query)
{
List<PhoneNumber> pNumber = new List<PhoneNumber>();
foreach (var PhoneNumbers in item.Elements("phone"))
{
pNumber.Add(new PhoneNumber(PhoneNumbers.Value,PhoneNumbers.Attribute("type").Value));
}
Adress cAdress = new Adress(item.Element("address").Element("street1").Value,
item.Element("address").Element("city").Value,
item.Element("address").Element("state").Value,
item.Element("address").Element("postal").Value);
output.Add(new Contact(item.Element("name").Value,pNumber,cAdress));
}
return output;
}
//subclasses
public class Adress
{
private string street;
private string city;
private string state;
private string postal;
public string Postal
{
get { return postal; }
}
public string State
{
get { return state; }
}
public string City
{
get { return city; }
}
public string Street
{
get { return street; }
}
public Adress(string _street,string _city, string _state, string _postal)
{
street = _street;
city = _city;
state = _state;
postal = _postal;
}
}
public class PhoneNumber
{
private string number;
private string numberType;
public string NumberType
{
get { return numberType; }
}
public string Number
{
get { return number; }
}
public PhoneNumber(string _number, string _phoneNumberType)
{
number = _number;
numberType = _phoneNumberType;
}
}
}
And I want to somehow bind to Gridview: (default.aspx)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"
SortExpression="Name" />
<asp:DynamicField DataField="CAdress" HeaderText="CAdress" />
<asp:DynamicField DataField="PNumber" HeaderText="PNumber" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get"
TypeName="LINQtoXML_WebForms.Contact"></asp:ObjectDataSource>
Any ideas how to bind for example Contact.PhoneNumber.number + "-" Contact.PhoneNumber.numberType to colomn?
i.e.: 774-6655-252 - mobile
Thanks.
When I run it, it shows me an error:
Could not determine a MetaTable. A MetaTable could not be determined for the data source 'ObjectDataSource1' and one could not be inferred from the request URL. Make sure that the table is mapped to the dats source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute.
Upvotes: 0
Views: 3290
Reputation: 13600
I think you either have to add meta information to your class or use the approach I'd use - write a method yourself instead of using objectdatasource object.
For example Linq to XML
public List<Contact> GetContacts()
{
XDocument doc = XDocument.Load("path_to_some_file.xml");
var contacts = from o in doc.Descendants("contact")
select new Contact()
{
Name = (string)o.Element("Name"),
Phone = (string)o.Element("Phone")
};
return contacts.ToList();
}
and then just bind it in code-behind to your control:
GridView1.DataSource = GetContacts();
GridView1.DataBind();
Hope you get the idea.
Upvotes: 2